Back to Practice
#0262
Largest Rectangle in Histogram
HardDSA40 min25 XP
Problem
Given bar heights, return the maximum rectangular area using contiguous bars.
Why This Matters
This is the advanced monotonic-stack problem that proves stacks can store unresolved boundaries, not just values.
Function Signature
def largest_rectangle_area(heights):
Examples
Example 1
Inputheights = [2,1,5,6,2,3]
Output10
Bars 5 and 6 form area 5 * 2 = 10.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Classic histogram
Input[2,1,5,6,2,3]
Expected10
The best rectangle spans the bars with heights 5 and 6.
Two bars
Input[2,4]
Expected4
The best area is either height 4 width 1 or height 2 width 2.
Hidden Test Categories
increasing heightsdecreasing heightsall equal heights