417. 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]]

Example 2:

Input: heights = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]

Constraints:

  • m == heights.length

  • n == heights[r].length

  • 1 <= m, n <= 200

  • 0 <= heights[r][c] <= 105

Solution

從ocean邊界開始尋找water source,當water source出現在兩個ocean的source list裡時,就是答案。

要成為water source 的條件是,比某個已知的water source地形高 or相等。

一開始default的water source為ocean 邊界 (因為一定有水)。

以此當初步條件進行BFS,也可以是DFS。往島內找,把所有可能的water source找出來。

class Solution {
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> ret = new LinkedList<>();
        int m = matrix.length;
        if(m == 0) return ret;
        int n = matrix[0].length;
        Queue<int[]> srcGridPacific = new LinkedList<>();
        Queue<int[]> srcGridAtlantic = new LinkedList<>();
        boolean[][] visitedAtlantic = new boolean[m][n];
        boolean[][] visitedPacific = new boolean[m][n];
        
        for(int col = 0; col < n; col++){
            srcGridPacific.add(new int[]{0, col});
            srcGridAtlantic.add(new int[]{m-1, col});
            visitedAtlantic[m-1][col] = true;
            visitedPacific[0][col] = true;
        }
        
        for(int row = 0; row < m; row++){
            srcGridPacific.add(new int[]{row, 0});
            srcGridAtlantic.add(new int[]{row, n-1});
            visitedPacific[row][0] = true;
            visitedAtlantic[row][n-1] = true;
        }
        bfs(matrix, srcGridPacific, visitedPacific);
        bfs(matrix, srcGridAtlantic, visitedAtlantic);
        
        for(int row = 0; row < m; row++){
            for(int col = 0; col < n; col++){
                if(visitedPacific[row][col] && visitedAtlantic[row][col])
                    ret.add(Arrays.asList(row, col));
            }
        }
        return ret;
    }
    
    
    public void bfs(int[][] matrix, Queue<int[]> src, boolean[][] vistedStatus){
        int[][] direction = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        int m = matrix.length;
        int n = matrix[0].length;
        while(!src.isEmpty()){
            int[] cur = src.poll();
            for(int[] dir : direction){
                int newRow = cur[0] + dir[0];
                int newCol = cur[1] + dir[1];
                if(newRow < 0 || newCol < 0 || newRow >= m || newCol >= n || vistedStatus[newRow][newCol] 
                   || matrix[newRow][newCol] < matrix[cur[0]][cur[1]]) continue;
                vistedStatus[newRow][newCol] = true;
                src.add(new int[]{newRow, newCol});
            }    
            
        }
        
    }
    
}

Last updated

Was this helpful?