heights is a bar chart: bar i is heights[i] units tall and one unit wide, and the bars stand side by side with no gaps. Return the area of the biggest rectangle that fits entirely inside the bars.
A rectangle may span several neighbouring bars, but it can be no taller than the shortest bar it covers.
The bars 6, 5, 7 are all at least 5 tall, so a rectangle 5 high and 3 wide fits over them: 15. Taking the 2 as well would drop the height to 2 (area 8), and the tallest bar alone gives only 7.
A bar of height 0 splits the chart: nothing can stretch across it, so each 3 stands alone.
Constraints
1 <= heights.length <= 10⁵0 <= heights[i] <= 10⁴Every candidate rectangle is limited by its shortest bar. For each bar, ask: how wide could a rectangle exactly this tall stretch before a shorter bar stops it on either side?
Every rectangle that fits is held down by the shortest bar it covers. So instead of asking "which span of bars?", ask, for each bar i: what's the widest rectangle that is exactly heights[i] tall? The best rectangle overall is one of those n candidates, because whatever rectangle wins, its shortest bar is one of the bars, and the widest rectangle at that bar's height is at least as big.
Time O(n)Space O(n)
The full walkthrough and code are part of Premium
15