Largest Rectangle in Histogram
Difficulty: Hard
You're given the heights of the bars in a histogram. Each bar has width 1, and the bars stand side by side with no gaps. Return the area of the largest rectangle that fits inside the histogram's outline.
Examples
Input: heights = [2, 1, 5, 6, 2, 3]
Output: 10
The largest rectangle uses the bars of height 5 and 6 (indexes 2 and 3): width 2, height min(5, 6) = 5, for an area of 10.
Input: heights = [2, 4]
Output: 4
Using just the bar of height 4 alone gives width 1 * height 4 = 4; using both bars gives width 2 * height 2 = 4. Either way, the best is 4.
Input: heights = [1, 1, 1, 1]
Output: 4
Using all four bars, limited to their shared height of 1, gives width 4 * height 1 = 4, better than using any smaller slice.
Constraints
1 <= heights.length <= 10^5
0 <= heights[i] <= 10^4
Approach
If a bar is the shortest in a rectangle, that rectangle stretches left and right until it hits a shorter bar. Expanding outward from every bar is correct but slow, because it re-scans the same bars repeatedly.
Instead, scan left to right with a stack of bar indexes whose heights strictly increase. When the current bar is shorter than the top of the stack:
- The top bar's right boundary is the current position.
- Its left boundary is the bar now below it on the stack.
- Pop it, compute its rectangle, and repeat while the top is taller than the current bar.
Then push the current index. A final zero-height bar at the end forces every remaining bar on the stack to be resolved.
Solutions
Brute Force — Expand Outward From Every Bar
Treat each bar as the shortest in the rectangle, and expand left and right while the neighbors are at least as tall.
function largestRectangleArea(heights) {
let best = 0;
const n = heights.length;
for (let i = 0; i < n; i++) {
let left = i;
while (left > 0 && heights[left - 1] >= heights[i]) left--;
let right = i;
while (right < n - 1 && heights[right + 1] >= heights[i]) right++;
const width = right - left + 1;
best = Math.max(best, width * heights[i]);
}
return best;
}Time: O(n²) in the worst case (e.g. when all bars are the same height) · Space: O(1)
Optimal — Monotonic Increasing Stack
Scan left to right with a stack of bar indexes whose heights strictly increase. When the current bar is shorter than the top bar, the top bar's rectangle is fully known. Pop it and compute its area: the current index is the right boundary, and the new stack top is the left boundary.
function largestRectangleArea(heights) {
const stack = []; // indexes with strictly increasing heights, bottom to top
let best = 0;
const n = heights.length;
for (let i = 0; i <= n; i++) {
const currentHeight = i === n ? 0 : heights[i];
while (stack.length > 0 && heights[stack[stack.length - 1]] > currentHeight) {
const height = heights[stack.pop()];
const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;
best = Math.max(best, height * width);
}
stack.push(i);
}
return best;
}Time: O(n), since each index is pushed and popped at most once · Space: O(n)