Pacific Atlantic Water Flow - Problem

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

Input & Output

Example 1 — Basic Island Grid
$ 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]]
💡 Note: These cells can flow water to both Pacific (top/left edges) and Atlantic (bottom/right edges). For example, cell (0,4) with height 5 can flow to Pacific through the top edge and to Atlantic through the right edge.
Example 2 — Simple Small Grid
$ Input: heights = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[0,2],[1,2],[2,0],[2,1],[2,2]]
💡 Note: Water flows from higher to lower or equal heights. Cell (0,2) can reach Pacific via top edge and Atlantic via right edge. Cell (2,0) can reach Pacific via left edge and Atlantic via bottom edge.
Example 3 — Single Cell
$ Input: heights = [[1]]
Output: [[0,0]]
💡 Note: A single cell borders both oceans (touches all edges), so water can flow to both Pacific and Atlantic oceans.

Constraints

  • m == heights.length
  • n == heights[r].length
  • 1 ≤ m, n ≤ 200
  • 0 ≤ heights[r][c] ≤ 105

Visualization

Tap to expand
Pacific Atlantic Water Flow INPUT Pacific Ocean (Top + Left) 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 Atlantic Ocean (Bottom + Right) = Can reach both oceans = Pacific Ocean = Atlantic Ocean ALGORITHM STEPS 1 Create Two Sets pacific_reachable and atlantic_reachable 2 DFS from Pacific Border Start from top row and left column cells 3 DFS from Atlantic Border Start from bottom row and right column cells 4 Find Intersection Cells in BOTH sets can reach both oceans Reverse DFS Logic Instead of checking if water flows DOWN to ocean, check if water can flow UP from ocean (height >= prev) Time: O(m*n) Space: O(m*n) FINAL RESULT Cells reaching both oceans: 5 4 4 5 6 7 5 Output Array: [[0,4], [1,3], [1,4], [2,2], [3,0], [3,1], [4,0]] 7 cells found Key Insight: By using reverse DFS from ocean boundaries (going uphill instead of downhill), we efficiently find all cells that can reach each ocean. The intersection of Pacific-reachable and Atlantic-reachable sets gives us cells that can flow to BOTH oceans. This avoids redundant computation from a naive approach. TutorialsPoint - Pacific Atlantic Water Flow | Reverse DFS from Ocean Boundaries
Asked in
Google 32 Amazon 28 Microsoft 15 Facebook 12
85.6K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen