34. Largest Rectangle in Histogram

heights describes a histogram whose bars have width 1. Return the area of the largest rectangle that fits inside it.

Example 1

Input: heights = [2,4,2,1]
Output: 6
Explanation: Height 2 across the first three bars: 2 × 3 = 6.

Example 2

Input: heights = [6,2,5,4,5,1,6]
Output: 12
Hint 1

For each bar, the widest rectangle of that height stretches until a shorter bar on each side.

Hint 2

A stack of increasing heights tells you both boundaries when a bar is popped.

Monotonic Stack