Skip to content
mlmentorship

Largest Rectangle in Histogram

Find the largest rectangle that fits under histogram bars.

Published · 8 min read ·Specialist ·Advanced

30-second answer map

Visual first · depth when needed

Keep increasing (start,height) candidates; the first shorter bar closes each taller candidate at its exact maximal width.

Preparing the visual…

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

How practice works

ML breadth · closed-book attempt

Largest Rectangle in Histogram

Explain the mechanism, why it works, when it fails, and one alternative.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Find the largest rectangle that fits under histogram bars.

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

Largest Rectangle in Histogram: Keep increasing (start,height) candidates; the first shorter bar closes each taller candidate at its exact maximal width.

Input and goalFind the largest rectangle that fits under histogram bars.
Initialize the increasing stackStart with an empty stack, best = 0, and scan the real histogram before an appended zero sentinel.
unresolved stackemptytop →
currentindex 0, height 2best0

Recognize it
Use it when each element needs the widest contiguous span for which it remains the limiting minimum, and the first smaller boundary finalizes that span.
Keep true
Stack heights are nondecreasing; each pair (start,height) can extend from start through index-1, and every popped height receives index as its first smaller right boundary while propagating its start to the incoming shorter bar.
Reuse it
When the first violating element determines an unresolved candidate’s right boundary, keep candidates monotone and finalize them in reverse order; propagate the oldest valid start across every pop.
Read it this way: Start with an empty stack, best = 0, and scan the real histogram before an appended zero sentinel. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.

Pattern: Increasing stack.

Simple idea: Save increasing heights and where each height began. A shorter bar ends the range of every taller saved bar. Calculate those areas when they end.

def largest_rectangle_area(heights: list[int]) -> int:
   stack: list[tuple[int, int]] = []
   best = 0

   for index, height in enumerate(heights + [0]):
      start = index
      while stack and stack[-1][1] > height:
         start, old_height = stack.pop()
         best = max(best, old_height * (index - start))
      stack.append((start, height))

   return best

Cost: time and space.