The Great Histogram
HardAcc. 34.2%
+150 XP 100
The Final Boss (Stage 1)
Given an array describing the heights of bars in a histogram, find the area of the largest rectangle that can be formed within the histogram.
The Assignment
Your function receives heights.
- For simplicity, use a nested loop strategy:
- For every bar
i, find the furthest left and furthest right you can go where heights are >=heights[i]. - Area =
heights[i] * (right - left + 1).
- For every bar
- Update and print the
maxArea.
01EXAMPLE 1
Input
[2,1,5,6,2,3]Output
10Explanation: Bars at index 2 and 3 (5 and 6) can form a rectangle of area 5 * 2 = 10.
Constraints
- Handle empty cases.
- A perfect O(n) solution uses a stack, but O(n^2) is accepted for now.
ArraysExpert
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
heights[2,1,5,6,2,3]
Expected Output
10
Click RUN to test your solution