26. Trapping Rain Water
HardArrays~35 min
An array represents an elevation map where each value is the height of a bar of width 1.
After it rains, water collects in the dips between bars. Return the total units of water trapped.
Examples
Example 1
- Input:
- heights = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
- Output:
- 6
- Why:
- Six units of water settle in the gaps between the taller bars.
Example 2
- Input:
- heights = [4, 2, 0, 3, 2, 5]
- Output:
- 9
Constraints
0 <= heights.length <= 100000 <= heights[i] <= 10000