Introduction
The Largest Rectangle in Histogram problem involves finding the maximum rectangular area inside a histogram.
Given:
- array of bar heights
Each bar:
- width = 1
The task is to:
- find largest rectangular area
possible inside histogram
This problem is one of the most important applications of:
Monotonic StackThis problem helps in understanding:
- previous smaller element
- next smaller element
- area optimization
- stack-based traversal
Example
Input:heights = [2,1,5,6,2,3]
Output: 10
Explanation:
Rectangle formed by:
5 and 6
Width = 2
Height = 5
Area = 10
Constraints
1 <= heights.length <= 10^50 <= heights[i] <= 10^4Approach 1 : Brute Force Expansion
Explanation
The simplest way is:
- Take every bar
- Expand left and right
- Find minimum height region
- Calculate maximum area
This works but repeated expansion becomes inefficient.
Steps
- Traverse every bar.
- Expand left side.
- Expand right side.
- Calculate area.
- Track maximum area.
Dry Run
Input:[2,1,5,6,2,3]
For height 5:
Expand: 5,6
Width = 2 Area = 5 × 2 = 10
Maximum Area: 10
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)Explanation:
Left and right expansions occur repeatedly.
Space Complexity: O(1) Explanation:
No extra structures used.
Approach 2 : Monotonic Stack
Explanation
The optimized solution uses:
Monotonic Increasing StackIdea:
- stack stores increasing heights
- whenever smaller height appears:
- rectangles are resolved
For each popped bar:
- current index gives:
- next smaller element
- stack top gives:
- previous smaller element
Area:
height × width This avoids repeated expansion.
Steps
- Create increasing stack.
- Traverse histogram.
- Resolve taller bars.
- Calculate rectangle area.
- Track maximum area.
Dry Run
Input:[2,1,5,6,2,3]
Stack:
[]
Push 2
Encounter 1
Pop 2
Width = 1
Area = 2
Push 1
Push 5
Push 6
Encounter 2
Pop 6 Area = 6
Pop 5 Width = 2 Area = 10
Maximum Area: 10
Monotonic Stack Code
Complexity Analysis
Time Complexity: O(n)
Explanation:
Each index is pushed and popped once.
Space Complexity: O(n) Explanation:
Stack stores indices.
Edge Cases
- Single bar
- Strictly increasing heights
- Strictly decreasing heights
- All equal heights
- Height = 0
Why This Problem is Important
Largest Rectangle in Histogram helps in understanding:
- Monotonic stacks
- Previous/next smaller elements
- Area optimization
- Stack-based geometry problems
- Efficient range calculations
It is one of the most important advanced stack interview problems.
Real-World Applications
Histogram optimization concepts are used in:
- Image processing
- Data visualization
- Resource allocation systems
- Memory optimization
- Skyline analysis
Common Mistakes
- Incorrect width calculation
- Forgetting final stack cleanup
- Wrong stack monotonic order
- Missing dummy height at end
Interview Tips
Interviewers often expect:
- Monotonic stack explanation
- Width calculation reasoning
- Previous/next smaller understanding
Always explain:
- why increasing stack is maintained
- how width is calculated
- why each bar is processed once
Related Questions
- Trapping Rain Water
- Next Greater Element
- Daily Temperatures
- Maximal Rectangle
- Sum of Subarray Minimums
Final Takeaway
The Largest Rectangle in Histogram problem is a fundamental advanced monotonic stack problem that teaches efficient area optimization and previous-next smaller element techniques. Understanding this problem builds a strong foundation for advanced stack and geometry-based interview problems.