Skip to content

Largest Rectangle in Histogram

HardStack
Decode it first. Clues, constraints, pattern and plan before any code.

A list heights describes a histogram: bar i is one unit wide and heights[i] tall, and the bars stand side by side. Return the area of the largest rectangle that fits entirely inside the histogram.

heights = [2, 1, 5, 6, 2, 3]   →  10   (bars 5 and 6, height 5, width 2)
heights = [2, 4]               →  4
heights = [2, 1, 2]            →  3    (height 1 across all three bars)

Constraints

  • 1 <= heights.length <= 10^5
  • 0 <= heights[i] <= 10^4