5 × 5 heights · flow to ≤ neighbour
1
2
2
3
5
3
2
3
4
4
2
4
5
3
1
6
7
1
4
5
5
1
1
2
4
Reverse DFS from both oceans
▸1flood(r, c, reach):2 reach[r][c] = true3 for each neighbour with height >= heights[r][c]:4 if not reach[nr][nc]: flood(nr, nc, reach)5seed Pacific from top row + left col6seed Atlantic from bottom row + right col7// intersect8answer = cells where pacific && atlantic9return answer
state
- ruleflow to ≤ neighbour
- trickreverse from oceans