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.
Examples
Constraints
m == heights.lengthn == heights[r].length1 <= m, n <= 2000 <= heights[r][c] <= 10^5
Reverse DFS from Oceans
Approach
Instead of starting from each cell and trying to reach the oceans, it is much more efficient to start from the oceans and see which cells can reach them by going "uphill" or to cells of equal height. We maintain two sets of visited cells: one for the Pacific and one for the Atlantic. We run DFS from all cells in the top row and left column (Pacific), and then from all cells in the bottom row and right column (Atlantic). Finally, we iterate through all cells and if a cell is in both visited sets, we add it to the result.
Complexity Analysis
Running DFS from every cell would take O((m * n)^2). By starting from the edges and going backwards, each cell is visited at most once for each ocean, making the time complexity O(m * n). Space complexity is O(m * n) for the visited sets and recursion stack.
class Solution { public List<List<Integer>> pacificAtlantic(int[][] heights) { List<List<Integer>> result = new ArrayList<>(); if (heights == null || heights.length == 0) return result; int m = heights.length; int n = heights[0].length; boolean[][] pacific = new boolean[m][n]; boolean[][] atlantic = new boolean[m][n]; // Run DFS from Pacific edges (top and left) for (int i = 0; i < m; i++) { dfs(heights, pacific, i, 0, heights[i][0]); } for (int j = 0; j < n; j++) { dfs(heights, pacific, 0, j, heights[0][j]); } // Run DFS from Atlantic edges (bottom and right) for (int i = 0; i < m; i++) { dfs(heights, atlantic, i, n - 1, heights[i][n - 1]); } for (int j = 0; j < n; j++) { dfs(heights, atlantic, m - 1, j, heights[m - 1][j]); } // Find intersection for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (pacific[i][j] && atlantic[i][j]) { result.add(Arrays.asList(i, j)); } } } return result; } private void dfs(int[][] heights, boolean[][] visited, int r, int c, int prevHeight) { int m = heights.length; int n = heights[0].length; if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c] || heights[r][c] < prevHeight) { return; } visited[r][c] = true; dfs(heights, visited, r + 1, c, heights[r][c]); dfs(heights, visited, r - 1, c, heights[r][c]); dfs(heights, visited, r, c + 1, heights[r][c]); dfs(heights, visited, r, c - 1, heights[r][c]); }}