Pacific Atlantic Water Flow - Problem

Imagine a mysterious rectangular island surrounded by two different oceans! ๐Ÿ๏ธ The Pacific Ocean laps against the island's left and top edges, while the Atlantic Ocean touches the right and bottom edges.

The island is divided into an m ร— n grid where each cell has a specific height above sea level. When it rains, water flows from higher cells to neighboring cells (north, south, east, west) that are at the same or lower height.

Your Mission: Find all cells where rainwater can flow to BOTH the Pacific and Atlantic oceans! Water can reach an ocean if it flows to any cell that borders that ocean.

Input: A 2D matrix heights where heights[r][c] represents the elevation at row r, column c.

Output: A list of coordinates [r, c] where water can reach both oceans.

Input & Output

example_1.py โ€” Basic Island
$ 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: The cell at (0,4) has height 5. Water can flow left to reach Pacific (through height 3โ†’2โ†’2โ†’1) and down to reach Atlantic (through height 4โ†’4โ†’1). Similarly, other cells in the result can reach both oceans through valid downhill paths.
example_2.py โ€” Single Row
$ Input: heights = [[1,2,3]]
โ€บ Output: [[0,2]]
๐Ÿ’ก Note: Only the rightmost cell (0,2) with height 3 can reach both oceans. It can reach Pacific by flowing left (3โ†’2โ†’1) and Atlantic directly since it borders the Atlantic.
example_3.py โ€” Uniform Heights
$ Input: heights = [[1,1],[1,1]]
โ€บ Output: [[0,0],[0,1],[1,0],[1,1]]
๐Ÿ’ก Note: Since all cells have the same height (1), water can flow freely between all cells. Every cell can reach both Pacific (top-left borders) and Atlantic (bottom-right borders).

Constraints

  • m == heights.length
  • n == heights[r].length
  • 1 โ‰ค m, n โ‰ค 200
  • 0 โ‰ค heights[r][c] โ‰ค 105
  • Water flows from higher or equal height to lower or equal height

Visualization

Tap to expand
๐ŸŒŠ Pacific Ocean๐ŸŒŠ Atlantic OceanHeight: 1Height: 2Height: 3Height: 5Height: 3Height: 2Height: 3Height: 4Height: 2Height: 4Height: 5Height: 3BOTH๐Ÿ’ก Key Insight:Reverse the flow direction!โ€ข Traditional: Cell โ†’ Ocean (slow)โ€ข Optimal: Ocean โ†’ Cell (fast)Algorithm Steps:1. DFS from Pacific borders โฌ†๏ธ2. DFS from Atlantic borders โฌ†๏ธ3. Find intersection ๐ŸŽฏComplexity:โฐ Time: O(m ร— n)๐Ÿ’พ Space: O(m ร— n)123532342453
Understanding the Visualization
1
Setup Island Grid
Create the island with height values and mark ocean borders
2
Pacific Flow Analysis
Start from Pacific borders (top/left) and trace uphill paths
3
Atlantic Flow Analysis
Start from Atlantic borders (bottom/right) and trace uphill paths
4
Find Dual-Drainage Points
Identify cells that can drain to both oceans
Key Takeaway
๐ŸŽฏ Key Insight: Instead of tracing water flow from every cell downhill to oceans (expensive), start from ocean borders and trace uphill to find all cells that can drain to each ocean (efficient)!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
180.0K Views
Medium Frequency
~25 min Avg. Time
4.2K 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