417. Pacific Atlantic Water Flow LeetCode Solution
In this guide, you will get 417. Pacific Atlantic Water Flow LeetCode Solution with the best time and space complexity. The solution to Pacific Atlantic Water Flow problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Pacific Atlantic Water Flow solution in C++
- Pacific Atlantic Water Flow solution in Java
- Pacific Atlantic Water Flow solution in Python
- Additional Resources

Problem Statement of Pacific Atlantic Water Flow
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.
Example 1:
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]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Constraints:
m == heights.length
n == heights[r].length
1 <= m, n <= 200
0 <= heights[r][c] <= 105
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
417. Pacific Atlantic Water Flow LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const int m = heights.size();
const int n = heights[0].size();
vector<vector<int>> ans;
queue<pair<int, int>> qP;
queue<pair<int, int>> qA;
vector<vector<bool>> seenP(m, vector<bool>(n));
vector<vector<bool>> seenA(m, vector<bool>(n));
auto bfs = [&](queue<pair<int, int>>& q, vector<vector<bool>>& seen) {
while (!q.empty()) {
const auto [i, j] = q.front();
q.pop();
const int h = heights[i][j];
for (const auto& [dx, dy] : dirs) {
const int x = i + dx;
const int y = j + dy;
if (x < 0 || x == m || y < 0 || y == n)
continue;
if (seen[x][y] || heights[x][y] < h)
continue;
q.emplace(x, y);
seen[x][y] = true;
}
}
};
for (int i = 0; i < m; ++i) {
qP.emplace(i, 0);
qA.emplace(i, n - 1);
seenP[i][0] = true;
seenA[i][n - 1] = true;
}
for (int j = 0; j < n; ++j) {
qP.emplace(0, j);
qA.emplace(m - 1, j);
seenP[0][j] = true;
seenA[m - 1][j] = true;
}
bfs(qP, seenP);
bfs(qA, seenA);
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (seenP[i][j] && seenA[i][j])
ans.push_back({i, j});
return ans;
}
};
/* code provided by PROGIEZ */
417. Pacific Atlantic Water Flow LeetCode Solution in Java
class Solution {
public List<List<Integer>> pacificAtlantic(int[][] heights) {
final int m = heights.length;
final int n = heights[0].length;
List<List<Integer>> ans = new ArrayList<>();
Queue<Pair<Integer, Integer>> qP = new ArrayDeque<>();
Queue<Pair<Integer, Integer>> qA = new ArrayDeque<>();
boolean[][] seenP = new boolean[m][n];
boolean[][] seenA = new boolean[m][n];
for (int i = 0; i < m; ++i) {
qP.offer(new Pair<>(i, 0));
qA.offer(new Pair<>(i, n - 1));
seenP[i][0] = true;
seenA[i][n - 1] = true;
}
for (int j = 0; j < n; ++j) {
qP.offer(new Pair<>(0, j));
qA.offer(new Pair<>(m - 1, j));
seenP[0][j] = true;
seenA[m - 1][j] = true;
}
bfs(heights, qP, seenP);
bfs(heights, qA, seenA);
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (seenP[i][j] && seenA[i][j])
ans.add(new ArrayList<>(List.of(i, j)));
return ans;
}
private static final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
private void bfs(int[][] heights, Queue<Pair<Integer, Integer>> q, boolean[][] seen) {
while (!q.isEmpty()) {
final int i = q.peek().getKey();
final int j = q.poll().getValue();
final int h = heights[i][j];
for (int[] dir : dirs) {
final int x = i + dir[0];
final int y = j + dir[1];
if (x < 0 || x == heights.length || y < 0 || y == heights[0].length)
continue;
if (seen[x][y] || heights[x][y] < h)
continue;
q.offer(new Pair<>(x, y));
seen[x][y] = true;
}
}
}
}
// code provided by PROGIEZ
417. Pacific Atlantic Water Flow LeetCode Solution in Python
class Solution:
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
m = len(heights)
n = len(heights[0])
qP = collections.deque()
qA = collections.deque()
seenP = [[False] * n for _ in range(m)]
seenA = [[False] * n for _ in range(m)]
for i in range(m):
qP.append((i, 0))
qA.append((i, n - 1))
seenP[i][0] = True
seenA[i][n - 1] = True
for j in range(n):
qP.append((0, j))
qA.append((m - 1, j))
seenP[0][j] = True
seenA[m - 1][j] = True
def bfs(q: deque, seen: list[list[bool]]):
while q:
i, j = q.popleft()
h = heights[i][j]
for dx, dy in dirs:
x = i + dx
y = j + dy
if x < 0 or x == m or y < 0 or y == n:
continue
if seen[x][y] or heights[x][y] < h:
continue
q.append((x, y))
seen[x][y] = True
bfs(qP, seenP)
bfs(qA, seenA)
return [[i, j] for i in range(m) for j in range(n) if seenP[i][j] and seenA[i][j]]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.