1. Climb From the Oceans 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.