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:
The order of returned grid coordinates does not matter.
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).

Solution:BFS

思路:两个ocean分别BFS染色
还要visitedcheck是因为有可能有==情况使得陷入死循环
Time Complexity: O(mn) Space Complexity: O(mn)

Solution Code:

class Solution {
    private final int[][] dir = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> result = new ArrayList<>();
        if(matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return result;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];
        
        Queue<int[]> paci_queue = new LinkedList<>();
        Queue<int[]> atlan_queue = new LinkedList<>();
        
        // vertical border init
        for(int x = 0; x < n; x++) {
            paci_queue.offer(new int[]{0, x});
            pacific[0][x] = true;
            atlan_queue.offer(new int[]{m - 1, x});
            atlantic[m - 1][x] = true;
        }
        // horizontal border init
        for(int y = 0; y < m; y++) {
            paci_queue.offer(new int[]{y, 0});
            pacific[y][0] = true;
            atlan_queue.offer(new int[]{y, n - 1});
            atlantic[y][n - 1] = true;
        }
        
        // dye
        bfs(matrix, paci_queue, pacific);
        bfs(matrix, atlan_queue, atlantic);
        
        // get reuslt
        for(int y = 0; y < m; y++) {
            for(int x = 0; x < n; x++) {
                if(pacific[y][x] && atlantic[y][x]) {
                    result.add(new int[]{y, x});
                }
            }
        }
        return result;
    }
    
    private void bfs(int[][] matrix, Queue<int[]> queue, boolean[][] visited) {
        int m = matrix.length;
        int n = matrix[0].length;
        while(!queue.isEmpty()) {
            int[] cur = queue.poll();
            for(int[] d: dir) {
                int y = cur[0] + d[0];
                int x = cur[1] + d[1];
                if(y < 0 || y > m - 1 || x < 0 || x > n - 1 || visited[y][x] || matrix[cur[0]][cur[1]] > matrix[y][x]) {
                    continue;
                }
                visited[y][x] = true;
                queue.offer(new int[]{y, x});
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容