LeetCode 417 Pacific Atlantic Water Flow - Example and Complexity Analysis

Walkthrough and analysis

From Pacific (top row, left column), DFS upward (to cells >= current).

Pacific-reachable cells: all cells water can flow from to reach Pacific.

From Atlantic (bottom row, right column), DFS upward.

Atlantic-reachable cells: all cells water can flow from to reach Atlantic.

Intersection: cells that can reach both.

Two DFS passes, each O(mn)O(m \cdot n). Total: O(mn)O(m \cdot n) time. Space: O(mn)O(m \cdot n) for visited sets.