Skip to content
Problem 407 · LeetCode 407(opens in a new tab)

Trapping Rain Water II

HardArrays

You are given a grid heightMap of whole numbers. Each cell is a square column of that height. Rain falls on the whole grid, and water runs off the outer edge.

Return the total volume of water that stays trapped on top of the columns. A cell holds water up to the lowest level at which the water could spill out of the grid.

Example 1

Input:  heightMap = [[3, 5, 4],
                     [5, 1, 5],
                     [4, 2, 5]]
Output: 1
Why:    the middle fills to 2, then spills over the 2 below it
2 more examples and the constraints

Example 2

Input:  heightMap = [[4, 4, 4, 4],
                     [4, 1, 2, 4],
                     [4, 4, 4, 4]]
Output: 5
Why:    both inner cells fill to 4: 3 + 2

Example 3

Input:  heightMap = [[2, 2, 2],
                     [2, 2, 2]]
Output: 0
Why:    every cell is on the border, so water runs off

Constraints

  • 1 ≤ rows, columns ≤ 200
  • 0 ≤ heightMap[r][c] ≤ 20,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Sweep until stableO((m·n)²)
  2. Min-heap from the borderO(m·n log(m·n))

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 200 × 200 gridVerdict
Sweep until stableO((m·n)²)
Mention it, then move on
Min-heap from the borderO(m·n log(m·n))
Best; each cell once

m = rows, n = columns.

Edge cases to test

  • Fewer than 3 rows or columns. Every cell is on the border, so the answer is 0.
  • A leak in the border. A low border cell drains the pool behind it, even if every other wall is high.
  • A tall cell inside. It holds nothing itself, and it can be the wall that sets a neighbour’s level.
  • Several pools. Pools can fill to different levels; each is set by its own lowest way out.

Common mistakes

  • Using the max of the left, right, up and down walls, as in 1D. Water can wind around and leak out elsewhere.
  • Pushing a neighbour with its own height instead of max(L, height), which lets water leak through a filled pool.
  • Marking a cell visited when it is popped, not when it is pushed, so it is counted twice.

How to talk through it

  1. 1Clarify. Does water run off every edge? Can heights be 0? Is a one-row grid possible?
  2. 2Show why 1D fails. In 2D the lowest way out may be far away and around a corner, so row and column maxima are not enough.
  3. 3Give the sweep idea. A cell’s level is max(height, lowest neighbour level). Repeat until stable; say why it can be slow.
  4. 4Code the heap. Start from the border, always grow from the lowest wall cell, and push each neighbour at max(L, height).
  5. 5Test out loud. Run Example 1, where the water leaks over the 2, and a one-row grid before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?