417. Pacific Atlantic Water Flow

Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.

Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.

Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.

Note:

  1. The order of returned grid coordinates does not matter.
  2. Both m and n are less than 150.

Example:

Given the following 5x5 matrix:

  Pacific ~   ~   ~   ~   ~ 
       ~  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  *
          *   *   *   *   * Atlantic

Return:

[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).

一刷
题解:
从太平洋和大西洋出发,通过dfs,标记途中的点,表示能到达大西洋/太平洋。

public class Solution {
    int[][] dirs = new int[][]{{0,1}, {1, 0}, {0, -1}, {-1, 0}};
    int n;
    int m;
    
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> res = new LinkedList<>();
        if(matrix==null || matrix.length ==0 || matrix[0].length == 0) return res;
        n = matrix.length;
        m = matrix[0].length;
        boolean[][] pacific = new boolean[n][m];
        boolean[][] atlantic = new boolean[n][m];
        for(int i=0; i<n; i++){
            dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);//first col
            dfs(matrix, atlantic, Integer.MIN_VALUE, i, m-1);//last col
        }
        for(int i=0; i<m; i++){
            dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);//first row
            dfs(matrix, atlantic, Integer.MIN_VALUE, n-1, i);//last row
        }
        
        for(int i=0;i<n; i++){
            for(int j=0; j<m; j++){
                if(pacific[i][j] && atlantic[i][j])
                    res.add(new int[]{i, j});
            }
        }
        
        return res;
    }
    
    private void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y){
        if(x<0 || y<0 || x>=n || y>=m || visited[x][y] || matrix[x][y]<height) return;
        visited[x][y] = true;
        for(int[] dir : dirs){
            dfs(matrix, visited, matrix[x][y], x+dir[0], y+dir[1]);
        }
    }
}

二刷
思路同上

class Solution {
    int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
    int m, n;
    public List<int[]> pacificAtlantic(int[][] matrix) {
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return new ArrayList<>();
        m = matrix.length;
        n = matrix[0].length;
        List<int[]> res = new ArrayList<>();
        boolean[][] pacifit = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];
        
        for(int i=0; i<m; i++){
            dfs(matrix, pacifit, i, 0);
            dfs(matrix, atlantic, i, n-1);
        }
        
        for(int j=0; j<n; j++){
            dfs(matrix, pacifit, 0, j);
            dfs(matrix, atlantic, m-1, j);
        }
        
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(pacifit[i][j] && atlantic[i][j]){
                    res.add(new int[]{i, j});
                }
            }
        }
        return res;
    }
    
    public void dfs(int[][] matrix, boolean[][] visited, int i, int j){
        visited[i][j] = true; 
        for(int[] dir:dirs){
            int x = i + dir[0];
            int y = j + dir[1];
            if(x<0 || x>=m || y<0 || y>=n || matrix[i][j]>matrix[x][y] || visited[x][y]) continue;
            dfs(matrix, visited, x, y);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容