Problem
An island has the shape of a grid with m rows and n columns. The value heights[r][c] is the height of the cell at row r and column c.
The Pacific Ocean touches the top edge and the left edge of the island. The Atlantic Ocean touches the bottom edge and the right edge.
Rain water flows from a cell to a neighbor cell when the neighbor is not higher. A neighbor cell is directly above, below, left or right. Water flows from a cell on an edge into the ocean at that edge.
Return the coordinates [r, c] of every cell from which water can flow to both oceans. You can return the coordinates in any order.
Example 1
- Input:
- heights = [[1,2,3],[8,9,4],[7,6,5]]
- Output:
- [[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
- Explanation:
- Only the cells
[0, 0]and[0, 1]are not in the answer. Water from them can flow only to the cell[0, 0], and all the neighbors of that cell are higher.
Example 2
- Input:
- heights = [[5]]
- Output:
- [[0,0]]
- Explanation:
- The single cell is on all four edges, so it touches both oceans.
Example 3
- Input:
- heights = [[2,2],[2,2]]
- Output:
- [[0,0],[0,1],[1,0],[1,1]]
- Explanation:
- Water flows between cells of equal height.
Constraints
- m == heights.length
- n == heights[r].length
- 1 <= m, n <= 200
- 0 <= heights[r][c] <= 10^5
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Calculate Trapping Rain Water
Coding
- Container With Most Water
Coding · Array · Greedy · Two Pointers
- Earliest Finish Time for Land and Water Rides I
Coding
- Earliest Finish Time for Land and Water Rides II
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 4 records
Amazon
Leetcode 417. Pacific Atlantic Water Flow
Reported Nov 14, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceMerge
Leetcode 417. Pacific Atlantic Water Flow
Reported Apr 7, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 417. Pacific Atlantic Water Flow
Reported Nov 27, 2024 · 1 source report
JUNIOR
Source snapshot: Nov 12, 2026
View sourcePinterest
Leetcode 417. Pacific Atlantic Water Flow
Reported Aug 4, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026