LeetCode 417 Pacific Atlantic Water Flow - Solution

The core idea

Reverse the flow direction. Instead of asking "where can water from this cell go?", ask "where can water reaching the Pacific/Atlantic come from?"

DFS/BFS from Pacific border (top row, left column) going to cells with >= height. Mark all reachable cells.

DFS/BFS from Atlantic border (bottom row, right column) similarly.

Cells in both sets can reach both oceans.