Certainly! Let's explain the core logic and intuition behind the implementation of finding the largest rectangle area in a histogram using a stack, as in your code.
Problem Restatement
You are given an array h where each element represents the height of a histogram bar (with width = 1 unit). You want to find the area of the largest rectangle that can be formed within the histogram bars.
Core Logic & Intuition
Intuition:
- Goal: For each bar
h[i], find the largest rectangle where h[i] is the minimum height in that rectangle.
- To find the largest rectangle involving
h[i], you want to extend to the left and right as far as possible while the bars are at least h[i] tall.
- The challenge is to efficiently find these boundaries for every bar.
Why a Stack?
A stack helps track bars in increasing order of heights. This structure allows us to know when a new bar breaks the increasing order, so we process the bars that were taller than the current one (and thus whose right boundary has been found).
Step-by-step Explanation:
-
Store increasing heights on the stack:
The stack stores indices of bars in ascending height order. When h[i] is greater or equal to the bar at the top of the stack, push i — this means the rectangle could potentially extend further for these bars.
-
Encounter a smaller height:
When the current bar h[i] is less than the bar on top of the stack, it means the rectangle with the height of the top bar can no longer extend to the right (because the current bar is shorter).
So, pop bars from the stack until the top of the stack bar is smaller or empty, while calculating the area of rectangles that end at i - 1.
-
Calculate area when popping:
- Height =
h[sk.top()] (height of the popped bar)
- Width = difference between the current index
i and the position of the new top of stack after popping, minus one.
- If the stack is empty after popping, width =
i (the whole span from 0 to i-1)
- Else width =
i - sk.top() - 1 (width between the next smaller bar on left + 1 and current index - 1)
- This width-height product gives the maximal rectangular area with the popped bar height.
-
Add a sentinel bar of height 0 at the end:
Append a 0 height bar at the end of h to flush out all remaining bars in the stack at the end of the iteration.
Walkthrough (Example):
Given heights: [2, 1, 5, 6, 2, 3]
- Start with an empty stack: push indices where heights increase.
- At
i=0 (h=2): stack empty, push 0
- At
i=1 (h=1): current height < h[top of stack]=2, pop 0
- height=2, width=1 (because stack is now empty, width = i = 1)
- area = 2*1=2
- Push
1 (index of height 1) now stack=[1]
- At
i=2 (h=5): 5 >= 1, push 2
- At
i=3 (h=6): 6 >= 5, push 3
- At
i=4 (h=2): 2 < 6, pop 3
- height=6, width=1 (
4 - 2 - 1) = 1
- area=6*1=6
2 < 5, pop 2
- height=5, width=2 (
4 - 1 -1) = 2
- area=5*2=10 -> max area updated to 10
- push
4 (height=2)
- At
i=5 (h=3): 3 >= 2, push 5
- Add height 0 at the end
h.push_back(0), now i=6 (h=0):
- pop
5
- height=3, width=1 (
6-4-1=1)
- pop
4
- height=2, width=4 (
6-1-1=4)
- area=2*4=8
- pop
1
- height=1, width=6 (
6 because stack empty)
- area=1*6=6
The largest area found is 10.
Summary:
- Use a stack to keep indices of bars with increasing heights.
- When current bar is shorter, pop from the stack and compute areas as you "close" rectangles.
- Maintain the max area seen throughout.
- Append
0 height bar at the end to process all bars.
This approach is efficient (O(n)) because each bar is pushed and popped at most once.
If you'd like, I can also provide a detailed diagram or code comments for this logic!