VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 03: Two pointers

Lesson 3.8 of 10 in this module Unrolled Pro

Trapping Rain Water

How much rain sits on an elevation map? From scanning left and right for every bar (O(n²)), to precomputed prefix and suffix maxima (O(n) time, O(n) space), to two pointers that carry the running maxima in O(1) space, with the reason the smaller side is always safe to settle. Then the stack view, a 2D map with a heap, and running maxima in ML monitoring.

about 10 min 4 Python examples 2 quick checks

Problem. height[i] is the height of a bar of width 1. After rain, how many units of water are trapped?

text
height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]   →   6
height = [4, 2, 0, 3, 2, 5]                     →   9

ML context. The core quantity is the running maximum from each side: "the highest point seen so far". The same prefix/suffix aggregate shows up when you track best-so-far validation loss for early stopping, compute the drawdown of a metric (how far it has fallen from its previous peak), or find regressions in a dashboard. The two-pointer trick shows how to get both sides' maxima without storing either array.


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 "Trapping Rain Water"

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