Maximal View Area
In urban development, understanding the "view area" for a building can be critical for factors like property valuation, light access, or even strategic drone placement. Imagine a city skyline where each building has a specific height. For any given building, we define its "view area" as the largest rectangular area it can form by extending horizontally to its left and right, constrained by the nearest buildings that are strictly taller than itself. If no strictly taller building exists on a side, the view extends to the edge of the city.
This problem challenges you to efficiently calculate the maximum possible "view area" among all buildings in the city.
Implement the function maximalViewArea that takes an array of non-negative integers heights representing the heights of buildings. Your task is to calculate the maximum "view area" among all possible buildings.
For each building i (at index i):
L_idx: the largest index l < i such that heights[l] > heights[i]. If no such building exists, L_idx = -1 (representing the left boundary of the city).R_idx: the smallest index r > i such that heights[r] > heights[i]. If no such building exists, R_idx = heights.length (representing the right boundary of the city).i is calculated as heights[i] * (R_idx - L_idx - 1).Your function should return the maximum "view area" found across all buildings.
heights: number[]
heights[p] represents the height of the building at index p.number
heights should result in a return value of 0.R_idx - L_idx - 1 correctly accounts for the indices and the inclusive nature of the building i itself.Examples
Example 1
Input:
heights = [2, 1, 5, 6, 2, 3]
Output:
36
Note:
Let N = 6.
The maximum area found is 36.
Example 2
Input:
heights = [1, 2, 3, 4]
Output:
16
Note:
Let N = 4.
The maximum area found is 16.
Example 3
Input:
heights = [5, 5, 5]
Output:
15
Note:
Let N = 3.
All buildings have the same height, so for each, its view extends to the city boundaries. The maximum area is 15.
Constraints
0 <= heights.length <= 10^50 <= heights[i] <= 10^9heights will only contain non-negative integers.Loading editor...