84. Largest Rectangle in Histogram
Asked at Microsoft, Bloomberg
Difficulty
Hard
Time
O(n)
Space
O(n)
Pattern
Array
Companies
2
Problem
You are given an array heights. It describes a histogram: each value is the height of a bar, and every bar has width 1. The bars sit side by side with no gaps.
Find the area of the largest rectangle that fits completely inside the histogram. The rectangle must be axis-aligned, and its top edge must be flat. So the rectangle's height is limited by the shortest bar it covers, and its width is the number of bars it covers.
Return that maximum area as an integer. If some bar has height 0, a rectangle can still be formed using other bars.
Pick the best flat-topped rectangle
Constraints
- ●
1 <= heights.length <= 10^5 - ●
0 <= heights[i] <= 10^4
Examples
heights = [2,1,5,6,2,3]10heights = [2,4]4Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Microsoft | Hard | View all Microsoft questions → |
| Bloomberg | Hard | View all Bloomberg questions → |
How to Think About It
Brute force: For every pair of bars, calculate the rectangle area using the minimum height in that range. O(n^3) with naive min finding.
Optimized brute force: For each bar, expand left and right to find the tallest bars that allow this bar as the minimum. O(n^2).
Monotonic stack: Maintain a stack of increasing heights. When a shorter bar is encountered, pop and compute area for each popped bar.
The popped bar extends from the previous stack top to the current index, giving left and right boundaries.
Single pass with monotonic stack achieves O(n) time and O(n) space.
Optimal Approach
Use a monotonic increasing stack of indices. Iterate through the bars with a sentinel 0 appended at the end. For each bar, while the stack top has a height greater than the current bar, pop it and calculate the area using the popped bar as the minimum height. The width extends from the new stack top to the current index. Track the maximum area across all such calculations.
Monotonic stack, one pass
What Trips People Up in Real Interviews
Confirm bar widths are uniform (typically 1 unit).
Explain the key insight: each bar is the minimum height for some range of consecutive bars.
Walk through the stack invariant: stack stores indices with strictly increasing heights.
Handle the final cleanup: pop remaining stack elements using n as the right boundary.
State time as O(n) because each element is pushed and popped at most once.
Solution Code
O(n) timedef largestRectangleArea(heights):
stack = []
max_area = 0
heights.append(0)
for i in range(len(heights)):
while stack and heights[stack[-1]] > heights[i]:
h = heights[stack.pop()]
w = i if not stack else i - stack[-1] - 1
max_area = max(max_area, h * w)
stack.append(i)
heights.pop()
return max_areaFrequently Asked Questions
What is the Largest Rectangle in Histogram problem?
Given an array of integers representing bar heights in a histogram where all bars have the same width, find the area of the largest rectangular bar that fits entirely within the histogram.
How do you solve Largest Rectangle in Histogram?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Largest Rectangle in Histogram?
Largest Rectangle in Histogram is asked at Microsoft, Bloomberg. It is a hard difficulty problem.
What are common mistakes on Largest Rectangle in Histogram?
- Confirm bar widths are uniform (typically 1 unit).
- Explain the key insight: each bar is the minimum height for some range of consecutive bars.
- Walk through the stack invariant: stack stores indices with strictly increasing heights.
- Handle the final cleanup: pop remaining stack elements using n as the right boundary.
- State time as O(n) because each element is pushed and popped at most once.