Monotonic stack · Hard
A storage visualization represents adjacent unit-width columns with different non-negative heights. A solid rectangle must fit below every column it spans. The report needs the largest area, not the number of columns or the height of the tallest one.
Return the maximum area of a rectangle spanning consecutive unit-width bars without exceeding any selected height.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ heights: integer[] }
Fields
- heights is the ordered array of non-negative integer bar heights. All bar widths are one.
Output
Return value
- Return a numeric area. An empty histogram or all-zero heights returns zero.
- The rectangle may span one or more consecutive bars. Its height is at most the shortest included bar.
Constraints
- 0 <= heights.length <= 100000; 0 <= heights[i] <= 1000000000. Areas fit a safe JSON integer.
Examples
Example 1
{
"heights": [
2,
1,
5,
6,
2,
3
]
}Returns
10Example 2
{
"heights": [
4,
4,
4
]
}Returns
12Required complexity
O(n) time and O(n) auxiliary space