Problem
Given an m by n map of heights, heights, the Pacific touches its top and left edges, and the Atlantic its bottom and right edges.
Rain flows from a cell to any neighbour up, down, left or right that is no higher, and off an edge into the ocean there.
Return every cell whose rain can reach both oceans, as row and column pairs, sorted by row and then by column.
Examples
Example 1:
Input: heights = [[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]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Example 3:
Input: heights = [[1,2],[4,3]]
Output: [[0,1],[1,0],[1,1]]
Explanation: the 1 at [0,0] is walled in by higher ground on both inland sides.
Example 4:
Input: heights = [[3,3,3],[3,1,3],[3,3,3]]
Output: [[0,0],[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[2,2]]
Explanation: the level ring reaches everywhere; the pit in the middle reaches nothing.
Constraints
1 <= m, n <= 200
0 <= heights[i][j] <= 10^5
How to think about it
1. Climb Up From Both Oceans Optimal
Intuition
Climb uphill from each ocean, and keep the cells both oceans reach. Following the rain down from every cell walks the same slopes again and again, so turn the question round: rain from a cell reaches an ocean exactly when you can climb from that ocean to the cell, each step onto a neighbour at least as high, because rain from there could run back down the same steps. Each ocean climbs from its own edges, starting at sea level 0, and records every cell it reaches; a cell it has already reached stops the climb, so each ocean records a cell once. Last, a scan row by row keeps the cells both oceans reached, which also puts the pairs in the order asked for.
Algorithm
1. Keep the heights, for climb to reach, and a record of the cells each ocean reaches.
2. For each row, climb the Pacific in from its left end and the Atlantic from its right end; for each column, the Pacific from its top and the Atlantic from its bottom, each from sea level 0.
3. In climb(r, c, reached, floor): off the map, return. Already reached, or lower than the floor, return.
4. Otherwise record the cell, and climb down, up, right and left, with the cell's height as the floor.
5. Scan row by row, and keep [r, c] for every cell both oceans reached.
Time & Space
Time O(m·n) for an m by n map: each ocean records a cell at most once and tries its four neighbours. Space O(m·n): the two records and the calls, which go as deep as the cells one climb reaches, so a huge flat map can overflow the call stack (in Python from about 1,000 cells, the default recursion limit). The second approach's Python keeps a stack of its own, which a huge map cannot overflow.
2. Check Each Step Before Taking It Optimal
Intuition
Asking "where can this cell drain to" from every cell re-walks the same paths endlessly. Reverse the question: start AT each ocean and climb to equal-or-higher neighbours, which marks everything that can drain into it. Two such floods, one per ocean, and the answer is the intersection — each cell is visited a constant number of times instead of starting its own search.
Algorithm
1. Flood from every Pacific-edge cell, moving only to equal-or-higher neighbours.
2. Flood the same way from every Atlantic edge.
3. Collect cells reached by both, walking the grid row-major for the required order.
Time & Space
Time O(m x n). Space O(m x n). The per-cell downhill search is O((mn)^2), which this replaces.