Trapping Rain Water II
Master this topic with zero to advance depth.
Trapping Rain Water II
Given an m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.
Visual Representation
heightMap =
[1, 4, 3, 1, 3, 2]
[3, 2, 1, 3, 2, 4]
[2, 3, 3, 2, 3, 1]
Water trapped at (1,1) (height 2): bounded by 3, 4, 3, 3. Can hold 1 unit.
Water trapped at (1,2) (height 1): bounded by 3, 2, 3, 3. Can hold 2 units.
Total trapped = 14 units.
Min-Heap expanding from boundaries:
Push all boundary cells to Min-Heap.
Always pop the lowest boundary (weakest link) to see if water can flow into its neighbors.Examples
Level I: Iterative Water Level Updates
Intuition
The water level at any cell (r, c) is determined by the maximum of its own height and the minimum water level of its neighbors. We can start by assuming every inner cell can hold infinite water, and boundary cells hold exactly their height. Then, we iteratively relax (update) the inner cells until the water levels stabilize (no more changes occur), similar to the Bellman-Ford algorithm.
Detailed Dry Run
Grid 3x3 (Heights): 3 3 3 3 1 3 3 3 3
Init Water: 3 3 3 3 INF 3 3 3 3
Iter 1: Water[1,1] = max(H[1,1]=1, min(W[0,1]=3, W[2,1]=3, W[1,0]=3, W[1,2]=3)) = max(1, min(3,3,3,3)) = 3 Changes = True. Next loop Changes = False. Trapped at (1,1) = Water[1,1] - H[1,1] = 3 - 1 = 2.
Level II: 4-Way Boundary Scanning (Upper Bound Heuristic)
Intuition
In the 1D version, the water level at any index is min(maxLeft, maxRight). We can extend this logic to 2D by calculating the maximum height seen from all four cardinal directions (Left, Right, Top, Bottom) for each cell. The water level at (r, c) can be at most the minimum of these four peaks. While this ignores diagonal leaks, it provides an heuristic that is faster than iterative relaxation.
Detailed Dry Run
Grid 3x3: 3 3 3 3 1 3 3 3 3 For cell (1,1):
- MaxLeft = 3, MaxRight = 3, MaxUp = 3, MaxDown = 3
- Level = min(3,3,3,3) = 3
- Trapped = 3 - 1 = 2 (Correct in this case).
Level III: Min-Heap + BFS (Optimal)
Intuition
Water flows from high places to low places, but its maximum volume is gated by the weakest link (lowest boundary) surrounding it. If we start from the outer boundaries and always process the lowest boundary segment inwards (using a Min-Heap), we simulate how water would spill over. If the neighbor is lower than our current boundary, it fills up with water to meet our boundary height.
Detailed Dry Run
Grid: 3 3 3 3 1 3 3 3 3
- Push all boundaries to Min-Heap: (3,0,0),(3,0,1)... Visited boundary.
- Pop (3,0,1). Check neighbor (1,1). It's unvisited. Height is 1.
- Inner height 1 < Boundary height 3. Trapped = 3 - 1 = 2.
- Push neighbor (1,1) into Heap with updated height
max(1, 3) = 3, mark visited. - Continue popping. Other neighbors of (1,1) are boundaries and visited. Return total trapped = 2.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.