Watch how a monotonic increasing stack finds the largest rectangle under the histogram in O(n) time.
Monotonic Stack LeetCode 84 ↗ Hardheights representing the histogram's bar heights where each bar has a width of 1,
find the area of the largest rectangle in the histogram.heights = [2,1,5,6,2,3], the largest rectangle has area 10
(formed by bars at indices 2 and 3, each of height 5, giving width 2 × height 5 = 10).fun largestRectangleArea(heights: IntArray): Int { val stack = ArrayDeque<Pair<Int, Int>>() var maxArea = 0 for ((i, h) in heights.withIndex()) { var start = i while (stack.isNotEmpty() && stack.last().second > h) { val (idx, height) = stack.removeLast() val area = height * (i - idx) maxArea = maxOf(maxArea, area) start = idx } stack.addLast(Pair(start, h)) } for ((idx, height) in stack) { val area = height * (heights.size - idx) maxArea = maxOf(maxArea, area) } return maxArea}
We maintain a monotonic increasing stack of (index, height) pairs. The stack
invariant is that heights are always in non-decreasing order from bottom to top. When we encounter a bar
that is shorter than the stack's top, we know the taller bar can no longer extend to the right — so
we pop it and compute the rectangle it could form.
max_area = 0.i with height h, set start = i.h, pop (idx, height). The rectangle for that bar has width i - idx and height height. Update max_area and set start = idx (the current bar can extend back to where the popped bar started).(start, h) onto the stack. The start index may be earlier than i because the current bar could extend left over all the popped bars.(idx, height), the rectangle has width len(heights) - idx.The stack only holds bars in increasing height order. A taller bar on top can always extend right as long as subsequent bars are at least as tall. The moment a shorter bar arrives, we resolve all taller bars on the stack because their maximum rightward extent is now known. This guarantees every bar is pushed and popped at most once, giving us O(n) amortized time.
Each bar is pushed onto the stack exactly once and popped at most once, so the total number of
stack operations is O(n). The stack itself can hold up to n entries in
the worst case (e.g., a sorted ascending array), giving O(n) space.