Stack pattern
Largest Rectangle in Histogram
Given an array of bar heights representing a histogram where each bar has width 1, return the area of the largest rectangle that can be formed within the histogram. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 84
- Hard
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
StackLeetCode 84
Hard
From the first idea to the best one
- 01 Brute force · grow each bar both ways O(n²)O(1) space
- 02 Optimized · monotonic increasing stack O(n)O(n) space
What you will learn
- How to read Largest Rectangle in Histogram and spot the stack pattern in it
- Brute force · grow each bar both ways: O(n²) time, O(1) space
- Optimized · monotonic increasing stack: O(n) time, O(n) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Stack
See Largest Rectangle in Histogram run, step by step
Sign in and this page opens on the animation, with all 8 Stack problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.