Bird
0
0
DSA Cprogramming~10 mins

Largest Rectangle in Histogram Using Stack in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Largest Rectangle in Histogram Using Stack
Start with empty stack
Iterate over histogram bars
While stack not empty and current bar height < height at stack top
Pop from stack
Calculate area with popped bar as smallest
Update max area if needed
Push current bar index to stack
After iteration, pop remaining bars and calculate area
Return max area
We use a stack to keep indices of bars in ascending height order. When a lower bar appears, we pop from stack and calculate areas with popped bars as smallest height, updating max area.
Execution Sample
DSA C
int largestRectangleArea(int* heights, int n) {
    int maxArea = 0, i = 0;
    int stack[n];
    int top = -1;
    while (i < n) {
        if (top == -1 || heights[i] >= heights[stack[top]]) stack[++top] = i++;
        else {
            int h = heights[stack[top--]];
            int w = top == -1 ? i : i - stack[top] - 1;
            maxArea = maxArea > h * w ? maxArea : h * w;
        }
    }
    while (top != -1) {
        int h = heights[stack[top--]];
        int w = top == -1 ? i : i - stack[top] - 1;
        maxArea = maxArea > h * w ? maxArea : h * w;
    }
    return maxArea;
}
Calculates largest rectangle area in histogram by using a stack to track bars and computing areas when a smaller bar is found.
Execution Table
StepOperationCurrent Index (i)Stack Content (indices)Popped IndexCalculated AreaMax AreaVisual State (Bars with indices)
1Push index 00[0]--0Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
2Push index 11[0,1]--0Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
3Pop index 1 (height 1 > current height 5?) No, push index 22[0,1,2]--0Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
4Push index 33[0,1,2,3]--0Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
5Pop index 3 (height 6 > current height 2?) Yes4[0,1,2]36 * (4-2-1)=66Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
6Pop index 2 (height 5 > current height 2?) Yes4[0,1]25 * (4-1-1)=1010Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
7Push index 44[0,1,4]--10Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
8Push index 55[0,1,4,5]--10Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
9End of array reached, pop index 56[0,1,4]53 * (6-4-1)=310Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
10Pop index 46[0,1]42 * (6-1-1)=810Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
11Pop index 16[0]11 * (6-0-1)=510Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
12Pop index 06[]02 * 6=1212Bars: [2(0),1(1),5(2),6(3),2(4),3(5)]
💡 Stack empty and all bars processed, max area found is 12
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12Final
i01234445666666
stack (indices)[][0][0,1][0,1,2][0,1,2,3][0,1,2][0,1][0,1,4][0,1,4,5][0,1,4][0,1][0][][]
maxArea0000061010101010101212
Key Moments - 3 Insights
Why do we pop from the stack when the current bar is lower than the bar at the top of the stack?
Because the current lower bar means the rectangle with the height of the bar at the top of the stack cannot extend further. We calculate the area for that bar now (see steps 5 and 6 in execution_table).
Why do we calculate width as 'i - stack[top] - 1' after popping?
Because the popped bar's rectangle extends from the index after the new top of the stack to the current index minus one. This width calculation is shown in steps 5, 6, 9, and 10.
Why do we continue popping after finishing iterating all bars?
Remaining bars in the stack can still form rectangles extending to the end of the histogram. We pop them to calculate their areas (steps 9 to 12).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the maxArea after popping index 2?
A6
B10
C8
D12
💡 Hint
Check the 'Max Area' column at step 6 in execution_table.
At which step does the stack become empty for the first time?
AStep 8
BStep 10
CStep 12
DStep 5
💡 Hint
Look at the 'Stack Content' column and find when it shows [].
If the histogram had all bars of equal height, how would the stack behave during iteration?
AStack would keep growing without popping until the end
BStack would pop frequently at each step
CStack would be empty after first push
DStack would alternate between empty and full
💡 Hint
Refer to the condition for pushing or popping in the code and how equal heights affect it.
Concept Snapshot
Largest Rectangle in Histogram Using Stack:
- Use a stack to store indices of bars in ascending height order.
- Iterate bars: push if current bar >= top stack bar height.
- If current bar < top stack bar height, pop and calculate area.
- Width = current index - stack top index - 1 after pop.
- After iteration, pop remaining bars to calculate areas.
- Max area updated during pops.
Full Transcript
This concept uses a stack to find the largest rectangle in a histogram. We keep indices of bars in ascending order. When a smaller bar appears, we pop from the stack and calculate the area with the popped bar as the smallest height. We update the maximum area found. After processing all bars, we pop remaining bars to calculate their areas. The stack helps track where rectangles can extend. The width for area calculation is found by subtracting indices around the popped bar. This method efficiently finds the largest rectangle in O(n) time.