VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 06: Stacks and queues

Lesson 6.8 of 10 in this module Unrolled Pro

Largest Rectangle in Histogram

The largest rectangle that fits under a histogram. From trying every range with a running minimum (O(n²)), to treating each bar as the shortest and expanding outwards (still O(n²) on flat input), to finding each bar's previous and next smaller bar with two monotonic-stack passes, and finally one pass where every pop closes a rectangle (O(n)). Then the largest rectangle of 1s in a grid, the longest window of free GPU memory for a job of a given size, and the largest square.

about 15 min 6 Python examples 2 quick checks

Problem. heights[i] is the height of bar i in a histogram where every bar has width 1. Return the area of the largest rectangle that fits entirely under the bars.

text
heights = [2, 1, 5, 6, 2, 3]   →   10      (height 5 across bars 2 and 3)
heights = [2, 4]               →   4
heights = [3, 3, 3]            →   9

A rectangle spanning bars l..r can be at most as tall as the shortest bar in that range: area = min(heights[l..r]) × (r − l + 1). That's the whole problem; the work is avoiding every (l, r) pair.

ML context. Picture a timeline of free GPU memory, one bar per minute. A job that needs m GB for d minutes fits exactly when there's a window of d minutes where every bar is ≥ m: a rectangle under the histogram. The largest rectangle is the biggest job (in GB-minutes) the gap can hold, and the same stack answers "the longest window for a job that needs m GB" for every m at once (Change B).


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Largest Rectangle in Histogram"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee