Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.
Key Insight: For each bar, we need to find how far left and right it can extend (until we hit a shorter bar). A monotonic increasing stack naturally gives us this information: when we pop a bar, the current bar is the right boundary, and the new stack top is the left boundary.
The width of the rectangle with height heights[popped] is:
i (extends from index 0)i - stack.top() - 1Trick: Add a sentinel bar of height 0 at the end to force all remaining bars to be processed.
heights = [2,1,5,6,2,3]10heights = [2,4]41 <= heights.length <= 10^50 <= heights[i] <= 10^4Click "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers