给定一个表示建筑物高度的数组。阵列具有从左到右的建筑物,如下图所示,计算面向日落的建筑物数。假定所有建筑物的高度都不同。
例子:
Input : arr[] = {7, 4, 8, 2, 9}
Output: 3
Explanation: As 7 is the first element, it can
see the sunset.
4 can't see the sunset as 7 is hiding it.
8 can see.
2 can't see the sunset.
9 also can see the sunset.
Input : arr[] = {2, 3, 4, 5}
Output : 4
询问:亚马逊专访
可以很容易地观察到,到目前为止,只有最大的元素才能看到阳光
也就是说,curr_max将看到阳光,然后只有大于curr_max的元素才能看到阳光。我们从左到右遍历给定的数组。我们跟踪到目前为止所看到的最大元素。只要元素超过当前最大值,就递增结果并更新当前最大值。
C++
// C++ program to count buildings that can
// see sunlight.
#include
using namespace std;
// Returns count buildings
// that can see sunlight
int countBuildings(int arr[], int n)
{
// Initialuze result (Note that first building
// always sees sunlight)
int count = 1;
// Start traversing element
int curr_max = arr[0];
for (int i = 1; i < n; i++) {
// If curr_element is maximum
// or current element is
// equal, update maximum and increment count
if (arr[i] > curr_max || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 7, 4, 8, 2, 9 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countBuildings(arr, n);
return 0;
}
Java
// Java program to count buildings that can
// see sunlight.
class Test {
// Returns count buildings that can see sunlight
static int countBuildings(int arr[], int n)
{
// Initialuze result (Note that first building
// always sees sunlight)
int count = 1;
// Start traversing element
int curr_max = arr[0];
for (int i = 1; i < n; i++) {
// If curr_element is maximum
// or current element
// is equal, update maximum and increment count
if (arr[i] > curr_max || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
// Driver method
public static void main(String[] args)
{
int arr[] = { 7, 4, 8, 2, 9 };
System.out.println(countBuildings(arr, arr.length));
}
}
Python3
# Python3 program to count buildings
# that can see sunlight.
# Returns count buildings that
# can see sunlight
def countBuildings(arr, n):
# Initialuze result (Note that first
# building always sees sunlight)
count = 1
# Start traversing element
curr_max = arr[0]
for i in range(1, n):
# If curr_element is maximum or
# current element is equal,
# update maximum and increment count
if (arr[i] > curr_max or arr[i] == curr_max):
count += 1
curr_max = arr[i]
return count
# Driver code
arr = [7, 4, 8, 2, 9]
n = len(arr)
print(countBuildings(arr, n))
# This code is contributed by Rohit.
C#
// C# program to count buildings that can
// see sunlight.
using System;
class GFG {
// Returns count buildings
// that can see sunlight
static int countBuildings(int[] arr, int n)
{
// Initialuze result (Note that first building
// always sees sunlight)
int count = 1;
// Start traversing element
int curr_max = arr[0];
for (int i = 1; i < n; i++) {
// If curr_element is maximum
// or current element
// is equal, update maximum
// and increment count
if (arr[i] > curr_max || arr[i] == curr_max) {
count++;
curr_max = arr[i];
}
}
return count;
}
// Driver method
public static void Main()
{
int[] arr = { 7, 4, 8, 2, 9 };
Console.Write(countBuildings(arr, arr.Length));
}
}
// This code is contributed by Rohit.
PHP
$curr_max || $arr[$i] == $curr_max)
{
$count++;
$curr_max=$arr[$i];
}
}
return $count;
}
// Driver code
$arr = array(7, 4, 8, 2, 9);
$n = sizeof($arr) / sizeof($arr[0]);
echo countBuildings($arr, $n);
// This code is contributed by
// Rohit
?>
Javascript
输出:
3