← All problems
Largest Rectangle in Histogram
Given an array of bar heights in a histogram, find the area of the largest rectangle that can be formed within the bars. The brute force is O(n^2); find the linear-time stack solution.
Complexity
Time O(n), Space O(n)
Common pitfalls
- Width miscalculation (use current index - stack top - 1)
- Forgetting the sentinel
Your solution
Log in to save your code and track progress.