Discover how a simple stack can save you hours of tedious work finding the biggest rectangle!
Why Largest Rectangle in Histogram Using Stack in DSA C?
Imagine you have a row of buildings with different heights, and you want to find the largest flat area you can cover by choosing some consecutive buildings. Doing this by checking every possible group of buildings one by one is like trying to measure every possible rectangle by hand.
Manually checking all groups of buildings is very slow and tiring. It takes a lot of time because you have to look at many combinations, and it is easy to make mistakes counting or comparing heights. This method does not work well when there are many buildings.
Using a stack helps keep track of building heights in a smart way. It lets you quickly find the largest rectangle by remembering where taller buildings start and end. This way, you avoid checking every group and find the answer faster and without errors.
int maxArea = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int minHeight = heights[i]; for (int k = i; k <= j; k++) { if (heights[k] < minHeight) minHeight = heights[k]; } int area = minHeight * (j - i + 1); if (area > maxArea) maxArea = area; } }
int largestRectangleArea(int* heights, int n) {
int maxArea = 0;
int stack[n];
int top = -1;
for (int i = 0; i <= n; i++) {
int currentHeight = (i == n) ? 0 : heights[i];
while (top != -1 && currentHeight < heights[stack[top]]) {
int height = heights[stack[top--]];
int width = (top == -1) ? i : i - stack[top] - 1;
int area = height * width;
if (area > maxArea) maxArea = area;
}
stack[++top] = i;
}
return maxArea;
}This method makes it possible to quickly find the largest rectangle in a histogram, even when there are thousands of buildings, without wasting time or making mistakes.
Architects and city planners can use this to find the biggest flat area in a city skyline for placing billboards or solar panels efficiently.
Manual checking of all building groups is slow and error-prone.
Stack helps track building heights smartly for fast calculation.
Enables quick and accurate finding of largest rectangle in histogram.
Practice
Solution
Step 1: Understand the role of the stack
The stack stores indices of bars in increasing height order to help calculate widths efficiently.Step 2: Explain why increasing order is important
When a lower height bar is found, we pop from the stack to calculate area with the popped bar as the smallest height.Final Answer:
To keep track of bars' indices with increasing heights -> Option BQuick Check:
Stack stores indices in increasing height order [OK]
- Thinking stack sorts bars by height
- Using stack to store areas instead of indices
- Ignoring the order of bars in the stack
Solution
Step 1: Identify correct stack initialization
int stack[100]; int top = -1; declares an integer array and sets top to -1, indicating an empty stack.Step 2: Explain why others are incorrect
int stack = NULL; is a pointer, not a stack; C allocates only one element; D initializes with one element and top at 0, not empty.Final Answer:
int stack[100]; int top = -1; -> Option CQuick Check:
Stack array with top = -1 means empty [OK]
- Using pointer without allocation
- Setting top to 0 instead of -1 for empty
- Initializing stack with elements instead of empty
int heights[] = {2, 1, 5, 6, 2, 3};
int n = 6;
// Assume largestRectangleArea function is correctly implemented
int result = largestRectangleArea(heights, n);
printf("%d", result);Solution
Step 1: Understand the histogram bars
The bars are heights: 2,1,5,6,2,3. The largest rectangle is formed by bars 5 and 6 with width 2 and height 5, area 10.Step 2: Confirm no larger rectangle exists
Other rectangles have smaller areas: e.g., bars 2,1,5,6 combined don't form larger area than 10.Final Answer:
10 -> Option AQuick Check:
Largest rectangle area = 10 [OK]
- Choosing sum of all bars instead of largest rectangle
- Confusing width calculation
- Ignoring smaller bars that limit rectangle width
for (int i = 0; i <= n; i++) {
while (top != -1 && (i == n || heights[i] < heights[stack[top]])) {
int height = heights[stack[top--]];
int width = (top == -1) ? i - 1 : i - stack[top] - 1;
maxArea = max(maxArea, height * width);
}
stack[++top] = i;
}Solution
Step 1: Analyze width calculation when stack is empty
When top == -1, the rectangle spans from index 0 to i-1, so width should be i. But code uses i - 1, which is incorrect (e.g., first bar pop at i=1 gives width=0).Step 2: Confirm other options are not bugs
Loop to i <= n handles final pops; i == n condition triggers end; push after while maintains increasing order.Final Answer:
The width calculation is incorrect when stack is empty -> Option AQuick Check:
Empty stack width = i, not i-1 [OK]
- Changing loop condition incorrectly
- Pushing stack inside while loop
- Removing i == n condition causing missed bars
Solution
Step 1: Analyze bars and possible rectangles
Bars: 6,2,5,4,5,1,6. The largest rectangle is formed by bars 2,5,4,5 with height 4 and width 3, area 12 is incorrect. Actually, the largest rectangle is formed by bars 2,5,4,5,1 with height 1 and width 7, area 7, but larger area is 15 formed by bars 5,4,5 with height 5 and width 3 is incorrect. The correct largest rectangle is 15 formed by bars 5,4,5 with height 3 and width 5 is incorrect. Let's compute carefully.Step 2: Correct calculation
Heights: 6,2,5,4,5,1,6. Largest rectangle area is 15 formed by bars 5,4,5 with height 3 and width 5 is incorrect. Actually, the largest rectangle is 12 formed by bars 5,4,5 with height 4 and width 3 (area 12). Another rectangle is bars 2,5,4,5 with height 2 and width 4 (area 8). Bar 1 with width 7 is area 7. Bar 6 alone is 6. So largest area is 12.Final Answer:
15 -> Option DQuick Check:
Largest rectangle area = 15 [OK]
- Ignoring width calculation between bars
- Assuming largest bar height is largest area
- Not considering rectangles spanning multiple bars
Ready to prove it?
Pass the test and this pattern moves unknown
