Data Structures19 sections · 729 units
Open in Course

Histogram - Key Observation

Extension limits

For each bar, ask: how far can it extend left and right without encountering a shorter bar?

If bar at index ii has height hh, and it can extend from index ll to rr, its maximum rectangle has area h×(rl+1)h \times (r - l + 1). The answer is the maximum area over all bars.

Finding the extension limits for all bars in O(n)O(n) is the challenge. Monotonic stacks do exactly this.