Search

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).

Solution:


class Solution {
    private static final int[][] dir = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    private int numRows;
    private int numCols;
    private int[][] landHeights;
    
    public List<List<Integer>> pacificAtlantic(int[][] matrix) 
    {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return new ArrayList<>();
        }
        numRows = matrix.length;
        numCols = matrix[0].length;
        landHeights = matrix;
        
        Queue<int[]> pacific = new LinkedList<>();
        Queue<int[]> atlantic = new LinkedList<>();
        
        for (int i = 0; i < numRows; i++) 
        {
            pacific.offer(new int[]{i, 0});
            atlantic.offer(new int[]{i, numCols - 1});
        }
        for (int i = 0; i < numCols; i++) 
        {
            pacific.offer(new int[]{0, i});
            atlantic.offer(new int[]{numRows - 1, i});
        }
        
       
        boolean[][] pacificReach = bfs(pacific);
        boolean[][] atlanticReach = bfs(atlantic);
        List<List<Integer>> commonCells = new ArrayList<>();
        for (int i = 0; i < numRows; i++) 
        {
            for (int j = 0; j < numCols; j++) 
            {
                if (pacificReach[i][j] && atlanticReach[i][j]) 
                    commonCells.add(List.of(i, j));
            }
        }
        return commonCells;
    }
    
    private boolean[][] bfs(Queue<int[]> queue) 
    {
        boolean[][] result = new boolean[numRows][numCols];
        while (!queue.isEmpty()) 
        {
            int[] cell = queue.poll();
            result[cell[0]][cell[1]] = true;
            for (int[] d : dir) 
            { 
                int x = cell[0] + d[0];
                int y = cell[1] + d[1];
                
                if (x < 0 || x >= numRows || y < 0 || y >= numCols) continue;
                if (result[x][y]) continue;
                if (landHeights[x][y] < landHeights[cell[0]][cell[1]]) continue;
                
                queue.offer(new int[]{x, y});
            }
        }
        return result;
    }
}


59 views0 comments

Recent Posts

See All

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.