Largest Rectangle in Histogram
Problem Statement:โ
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.
Example 1:

Input: heights = [2,1,5,6,2,3]
Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
Example 2:

Input: heights = [2,4]
Output: 4
-
Example:
โ Solution: Monotonic Stack โ Largest Rectangle in Histogramโ
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int maxArea = 0;
stack<int> st;
int n = heights.size();
for (int i = 0; i <= n; i++) {
while (!st.empty() && (i == n || heights[st.top()] >= (i == n ? 0 : heights[i]))) {
int height = heights[st.top()];
st.pop();
int width = st.empty() ? i : i - st.top() - 1;
maxArea = max(maxArea, height * width);
}
st.push(i);
}
return maxArea;
}
};
๐ How It Worksโ
-
Goal: Find the largest rectangular area in a histogram.
-
Technique:
Use a monotonic increasing stack to keep track of bar indices.
-
Step-by-Step:
- Traverse from left to right.
- When the current bar is lower than the bar at the stack's top, we pop from the stack.
- For each popped bar, calculate the width:
- If stack is empty โ width =
i(from beginning toi - 1). - Otherwise โ width =
i - st.top() - 1(between two smaller bars).
- If stack is empty โ width =
- Update
maxAreausingheight * width. - Push the current index onto the stack.
-
Why
i == nloop condition:We need one extra iteration to flush out remaining bars in the stack after processing all indices.
๐งฉ Key Formulaโ
For each popped bar height = heights[st.top()]:
width = (st.empty() ? i : i - st.top() - 1)area = height * widthmaxArea = max(maxArea, area)
โฑ๏ธ Time & Space Complexityโ
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Monotonic Stack | O(N) | O(N) |
- Every bar is pushed and popped at most once.
โ ๏ธ Edge Casesโ
- All bars of same height.
- Increasing heights (e.g.,
[1, 2, 3, 4, 5]). - Decreasing heights (e.g.,
[5, 4, 3, 2, 1]). - Single bar โ Area is just height itself.
๐ก Other Approachesโ
| Approach | Time Complexity | Notes |
|---|---|---|
| Brute Force | O(Nยฒ) | Check all rectangles for each height. Too slow for large N. |
| Divide & Conquer | O(N log N) | Recursive max-min partitioning approach. Harder to implement. |
๐ Related Problemsโ
- LeetCode 84: Largest Rectangle in Histogram (Exact Problem)
- LeetCode 85: Maximal Rectangle
- LeetCode 42: Trapping Rain Water
- LeetCode 907: Sum of Subarray Minimums
๐ ๏ธ Other Notesโ
-
โ This is a classic monotonic stack template.
-
โ Real-world analogy:
Finding the largest rectangular billboard possible along a skyline of buildings.
-
โ Remember: Always push indices instead of values to calculate widths properly!
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.