Tuesday, September 8, 2020

LeetCode [417] Pacific Atlantic Water Flow

417. Pacific Atlantic Water Flow
Medium

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).
class Solution {
    int[][] dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> ret = new ArrayList<>();
        if(matrix == null) return ret;
        int m = matrix.length;
        if(m==0) return ret;
        int n = matrix[0].length;
        if(n==0) return ret;

        Queue<int[]> queueP = new LinkedList<>();
        Queue<int[]> queueA = new LinkedList<>();
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];

        for(int i=0; i<m; ++i)
        {
            queueP.offer(new int[]{i, 0});
            queueA.offer(new int[]{i, n-1});
            pacific[i][0] = true;
            atlantic[i][n-1] = true;
        }

        for(int j=0; j<n; ++j)
        {
            queueP.offer(new int[]{0, j});
            queueA.offer(new int[]{m-1, j});
            pacific[0][j] = true;
            atlantic[m-1][j] = true;
        }

        bfs(pacific, queueP, matrix);
        bfs(atlantic, queueA, matrix);

        for(int i=0; i<m; ++i)
        {
            for(int j=0; j<n; ++j)
            {
                if(pacific[i][j] && atlantic[i][j])
                {
                    ret.add(Arrays.asList(new Integer[] {i, j}));
                }
            }
        }

        return ret;
    }

    void bfs(boolean[][] visited, Queue<int[]> que, int[][] matrix)
    {
        int m = matrix.length, n = matrix[0].length;
        while(!que.isEmpty())
        {
            int[] cor = que.poll();
            for(int[] d : dir)
            {
                int x = cor[0] + d[0];
                int y = cor[1] + d[1];
                if(x>=0 && x<m && y>=0 && y<n && !visited[x][y] && matrix[cor[0]][cor[1]]<=matrix[x][y])
                {
                    visited[x][y] = true;
                    que.offer(new int[]{x, y});
                }
            }
        }
    }
}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution {
    int[][] dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        List<List<Integer>> ret = new ArrayList<>();
        if(matrix == null) return ret;
        int m = matrix.length;
        if(m==0) return ret;
        int n = matrix[0].length;
        if(n==0) return ret;

        Queue<int[]> queueP = new LinkedList<>();
        Queue<int[]> queueA = new LinkedList<>();
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];

        for(int i=0; i<m; ++i)
        {
            queueP.offer(new int[]{i, 0});
            queueA.offer(new int[]{i, n-1});
            pacific[i][0] = true;
            atlantic[i][n-1] = true;
        }

        for(int j=0; j<n; ++j)
        {
            queueP.offer(new int[]{0, j});
            queueA.offer(new int[]{m-1, j});
            pacific[0][j] = true;
            atlantic[m-1][j] = true;
        }

        bfs(pacific, queueP, matrix);
        bfs(atlantic, queueA, matrix);

        for(int i=0; i<m; ++i)
        {
            for(int j=0; j<n; ++j)
            {
                if(pacific[i][j] && atlantic[i][j])
                {
                    ret.add(Arrays.asList(new Integer[] {i, j}));
                }
            }
        }

        return ret;
    }

    void bfs(boolean[][] visited, Queue<int[]> que, int[][] matrix)
    {
        int m = matrix.length, n = matrix[0].length;
        while(!que.isEmpty())
        {
            int[] cor = que.poll();
            for(int[] d : dir)
            {
                int x = cor[0] + d[0];
                int y = cor[1] + d[1];
                if(x>=0 && x<m && y>=0 && y<n && !visited[x][y] && matrix[cor[0]][cor[1]]<=matrix[x][y])
                {
                    visited[x][y] = true;
                    que.offer(new int[]{x, y});
                }
            }
        }
    }
}

No comments:

Post a Comment