algorythms
Two Pointers
LC #42Hard

Trapping Rain Water

Two Pointers
AmazonGoogleMetaBloombergApple

Problem

Given an elevation map, compute how much water it can trap after raining.

arraytwo-pointersdynamic-programming

Constraints

  • 1 ≤ n ≤ 2 × 10⁴
  • 0 ≤ height[i] ≤ 10⁵

Example

Inputheight = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output6
Why

Trapped water at each index (0-based): 0,0,1,0,1,2,1,0,0,1,0,0 → total = 6

Hints — reveal one at a time