📌  相关文章
📜  从右数以不同颜色严格增加的建筑物数

📅  最后修改于: 2021-04-27 19:48:53             🧑  作者: Mango

给定一个整数K  和两个整数数组H []C []的大小K  其中H []存储连续建筑物的高度,而C []存储为其绘制建筑物的颜色代码。
任务是确定从右侧视图(即最右侧建筑物的右侧)一次可以看到多少种颜色。
例子:

方法:仔细观察后,可以简化上述问题,以从正确的角度找到颜色严格增加的建筑物数量。

  1. 将Height数组的Last元素存储在max变量中。
  2. 现在,在数组Arr中,在与颜色数组存储1的最后一个元素相对应的位置。
  3. 现在开始将Height数组从n-2遍历到0
  4. 如果我们得到的元素大于max,则将该变量存储在max中,然后再次存储在数组Arr中,其位置对应于颜色数组存储1中第i个元素。
  5. 最后,计算数组Arr中存在的1的数目。它给出了从末尾可见的颜色总数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to return the number of
// colors visible
int colourVisible(int height[], int colour[], int K)
{
    int arr[K + 1] = { 0 }, visible = 0;
 
    int max = height[K - 1];
    arr[colour[K - 1]] = 1;
 
    for (int i = K - 2; i >= 0; i--) {
        if (height[i] > max) {
            max = height[i];
            arr[colour[i]] = 1;
        }
    }
 
    // Count the Number of 1's
    for (int i = 1; i <= K; i++) {
        if (arr[i] == 1)
            visible++;
    }
 
    return visible;
}
 
// Driver code
int main()
{
    int height[] = { 3, 5, 1, 2, 3 };
    int colour[] = { 1, 2, 3, 4, 3 };
    int K = sizeof(colour) / sizeof(colour[0]);
 
    cout << colourVisible(height, colour, K);
 
    return 0;
}


Java
//Java  implementation of above approach
 
import java.io.*;
 
class GFG {
    // Function to return the number of
// colors visible
static int colourVisible(int height[], int colour[], int K)
{
    int arr[]=new int[K + 1] ;
    int visible = 0;
 
    int max = height[K - 1];
    arr[colour[K - 1]] = 1;
 
    for (int i = K - 2; i >= 0; i--) {
        if (height[i] > max) {
            max = height[i];
            arr[colour[i]] = 1;
        }
    }
 
    // Count the Number of 1's
    for (int i = 1; i <= K; i++) {
        if (arr[i] == 1)
            visible++;
    }
 
    return visible;
}
 
// Driver code
     
    public static void main (String[] args) {
     
    int height[] = { 3, 5, 1, 2, 3 };
    int colour[] = { 1, 2, 3, 4, 3 };
    int K = colour.length;
 
    System.out.println (colourVisible(height, colour, K));
    }
}


Python3
# Python3 implementation of above approach
 
# Function to return the number of
# colors visible
def colourVisible(height, colour, K):
    arr = [0 for i in range(K + 1)]
    visible = 0
 
    max = height[K - 1]
    arr[colour[K - 1]] = 1
     
    i = K - 2
    while(i >= 0):
        if (height[i] > max):
            max = height[i]
            arr[colour[i]] = 1
        i -= 1
     
    # Count the Number of 1 complement
    for i in range(1, K + 1, 1):
            if (arr[i] == 1):
                visible += 1
     
    return visible
 
# Driver code
if __name__ == '__main__':
    height = [3, 5, 1, 2, 3]
    colour = [1, 2, 3, 4, 3]
    K = len(colour)
 
    print(colourVisible(height, colour, K))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of above approach
using System;
 
class GFG
{
// Function to return the number of
// colors visible
static int colourVisible(int []height,
                         int []colour, int K)
{
    int []arr=new int[K + 1] ;
    int visible = 0;
 
    int max = height[K - 1];
    arr[colour[K - 1]] = 1;
 
    for (int i = K - 2; i >= 0; i--)
    {
        if (height[i] > max)
        {
            max = height[i];
            arr[colour[i]] = 1;
        }
    }
 
    // Count the Number of 1's
    for (int i = 1; i <= K; i++)
    {
        if (arr[i] == 1)
            visible++;
    }
 
    return visible;
}
 
// Driver code
static public void Main ()
{
    int []height = { 3, 5, 1, 2, 3 };
    int []colour = { 1, 2, 3, 4, 3 };
    int K = colour.Length;
     
    Console.WriteLine(colourVisible(height, colour, K));
}
}
 
// This code is contributed by Sach_Code


PHP
= 0; $i--)
    {
        if ($height[$i] > $max)
        {
            $max = $height[$i];
            $arr[$colour[$i]] = 1;
        }
    }
 
    // Count the Number of 1's
    for ($i = 1; $i <= $K; $i++)
    {
        if ($arr[$i] == 1)
            $visible++;
    }
 
    return $visible;
}
 
// Driver code
$height = array( 3, 5, 1, 2, 3 );
$colour = array( 1, 2, 3, 4, 3 );
$K = count($colour);
 
echo colourVisible($height, $colour, $K);
 
// This code is contributed by mits
?>


Javascript


输出:
2
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”