Skip to content
Decoder

Largest Rectangle in Histogram

Hard

Problem

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.

Example 1

Input
heights = [2, 1, 5, 6, 2, 3]
Output
10
Why
bars 5 and 6, height 5, width 2

Example 2

Input
heights = [2, 4]
Output
4

Example 3

Input
heights = [2, 1, 2]
Output
3
Why
height 1 across all three bars

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.