417. Pacific Atlantic Water Flow

Problem:
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).
Analysis:
Reversed BFS.

  1. Add pacific cells and alt antic cells to two queues.
  2. bfs separately
  3. The result is the cells that the above bfs both visited.
Solution:

class Solution {
    public List<int[]> pacificAtlantic(int[][] matrix) {
        List<int[]> res = new ArrayList<int[]>();
        if (matrix == null ||  matrix.length == 0 || matrix[0].length == 0 )
            return res;

        int m = matrix.length;
        int n = matrix[0].length;

        // pacific
        Queue<int[]> queue1 = new LinkedList<>();
        // atlantic
        Queue<int[]> queue2 = new LinkedList<>();
        boolean[][] visited1 = new boolean[m][n];
        boolean[][] visited2 = new boolean[m][n];

        for (int i = 0; i < m; i++) {
            queue1.offer(new int[]{i, 0});
            visited1[i][0] = true;
            queue2.offer(new int[]{i, n - 1});
            visited2[i][n - 1] = true;
        }

        for (int i = 0; i < n; i ++) {
            queue1.offer(new int[]{0, i});
            visited1[0][i] = true;
            queue2.offer(new int[] {m - 1, i});
            visited2[m - 1][i] = true;
        }

        bfs(matrix, queue1, m, n, visited1);
        bfs(matrix, queue2, m, n, visited2);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                if (visited1[i][j] && visited2[i][j]) {
                    res.add(new int[]{i, j});
                }
        }

        return res;
    }

    private void bfs(int[][] matrix, 
                                 Queue<int[]> queue,
                                 int m, 
                                 int n,
                                 boolean[][] visited) {
        int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int x = cur[0];
            int y = cur[1];
            for (int[] dir: dirs) {
                int newx = x + dir[0];
                int newy = y + dir[1];
                int newIndex = newx * n + newy;
                if (newx < 0 || newx >= m || newy < 0 || newy >= n || visited[newx][newy]) 
                    continue;
                if (matrix[x][y] > matrix[newx][newy]) {
                    continue;
                }
                queue.offer(new int[]{newx, newy});
                visited[newx][newy] = true;
            }
        }
    }
}

评论

此博客中的热门博文

663. Equal Tree Partition

776. Split BST

426. Convert Binary Search Tree to Sored Doubly Linked List