417. Pacific Atlantic Water Flow

Description

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:


417. Pacific Atlantic Water Flow_第1张图片
ocean

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

Solution

BFS, time O(mn), space O(mn)

这道题其实就是升级版的"Surrounded Region"或者"Wall and Gates",相比于从每个岛屿节点开始,不如从每个边缘节点(海洋)开始流。

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    public List pacificAtlantic(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return Collections.EMPTY_LIST;
        }
        
        int m = matrix.length;
        int n = matrix[0].length;
        
        Set pacific = new HashSet<>();
        Queue pacificQueue = new LinkedList<>();
        Set atlantic = new HashSet<>();
        Queue atlanticQueue = new LinkedList<>();
        
        for (int j = 0; j < n; ++j) {
            pacificQueue.offer(new int[] {0, j}); 
            pacific.add(get1DIndex(0, j, n));
            atlanticQueue.offer(new int[] {m - 1, j});
            atlantic.add(get1DIndex(m - 1, j, n));
        }
        
        for (int i = 0; i < m; ++i) {
            pacificQueue.offer(new int[] {i, 0});
            pacific.add(get1DIndex(i, 0, n));
            atlanticQueue.offer(new int[] {i, n - 1});
            atlantic.add(get1DIndex(i, n - 1, n));
        }
        
        bfs(matrix, pacificQueue, pacific);
        bfs(matrix, atlanticQueue, atlantic);
        
        List res = new ArrayList<>();
        for (int i : pacific) {
            if (atlantic.contains(i)) {
                res.add(get2DIndex(i, n));
            }
        }
        
        return res;
    }
    
    private void bfs(int[][] matrix, Queue queue, Set ocean) {
        int m = matrix.length;
        int n = matrix[0].length;
        
        while (!queue.isEmpty()) {
            int[] pos = queue.poll();
            
            for (int[] d : DIRECTIONS) {
                int x = pos[0] + d[0];
                int y = pos[1] + d[1];
                
                if (x < 0 || x >= m || y < 0 || y >= n 
                    || matrix[x][y] < matrix[pos[0]][pos[1]] 
                    || ocean.contains(get1DIndex(x, y, n))) {
                    continue;
                }
                
                queue.offer(new int[] {x, y});
                ocean.add(get1DIndex(x, y, n));
            }
        }
    }
    
    private int get1DIndex(int i, int j, int n) {
        return i * n + j;
    }
    
    private int[] get2DIndex(int i, int n) {
        return new int[] {i / n, i % n};
    }
}

DFS, time O(mn), space O(mn)

跟BFS相同的思路,从每个沿海的position开始探索即可。

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    public List pacificAtlantic(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return Collections.EMPTY_LIST;
        }
        
        int m = matrix.length;
        int n = matrix[0].length;
        
        Set pacific = new HashSet<>();
        Set atlantic = new HashSet<>();
        
        for (int j = 0; j < n; ++j) {
            dfs(matrix, 0, j, pacific);
            dfs(matrix, m - 1, j, atlantic);
        }
        
        for (int i = 0; i < m; ++i) {
            dfs(matrix, i, 0, pacific);
            dfs(matrix, i, n - 1, atlantic);
        }
        
        List res = new ArrayList<>();
        for (int i : pacific) {
            if (atlantic.contains(i)) {
                res.add(get2DIndex(i, n));
            }
        }
        
        return res;
    }
    
    private void dfs(int[][] matrix, int i, int j, Set ocean) {
        int m = matrix.length;
        int n = matrix[0].length;
        ocean.add(get1DIndex(i, j, n));
        
        for (int[] d : DIRECTIONS) {
            int x = i + d[0];
            int y = j + d[1];

            if (x < 0 || x >= m || y < 0 || y >= n 
                || matrix[x][y] < matrix[i][j] 
                || ocean.contains(get1DIndex(x, y, n))) {
                continue;
            }

            dfs(matrix, x, y, ocean);
        }
    }
    
    private int get1DIndex(int i, int j, int n) {
        return i * n + j;
    }
    
    private int[] get2DIndex(int i, int n) {
        return new int[] {i / n, i % n};
    }
}

你可能感兴趣的:(417. Pacific Atlantic Water Flow)