Two PointersHard

Trapping Rain Water

Each number is the height of a 1-wide column. After rain, water sits on top of columns wherever higher columns stand on both sides. Return the total water held.

Example 1heights = [3, 0, 2, 0, 4] → 73 + 1 + 3 units on the three low columns.
Example 2heights = [1, 2, 3] → 0
1 ≤ n ≤ 2·10⁴ · 0 ≤ heights[i] ≤ 10⁵

Hints

0 of 3 open
Hint 2 · the patternHint 3 · the key line