Skip to content

Latest commit

 

History

History

417

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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

 

Related Topics:
Depth-first Search, Breadth-first Search

Solution 1.

We can also use a two bits for each cell to represent if it's reachable from top-left and/or bottom-right. Here for simplicity I just used two vector<vector<int>>.

// OJ: https://leetcode.com/problems/pacific-atlantic-water-flow/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class Solution {
    int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}, M, N;
    void dfs(vector<vector<int>> &A, int x, int y, vector<vector<int>> &m) {
        if (m[x][y]) return;
        m[x][y] = 1;
        for (auto &[dx, dy] : dirs) {
            int a = x + dx, b = y + dy;
            if (a < 0 || a >= M || b < 0 || b >= N || A[a][b] < A[x][y]) continue;
            dfs(A, a, b, m);
        }
    }
public:
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& A) {
        if (A.empty() || A[0].empty()) return {};
        M = A.size(), N = A[0].size();
        vector<vector<int>> a(M, vector<int>(N)), b(M, vector<int>(N)), ans; 
        for (int i = 0; i < M; ++i) {
            dfs(A, i, 0, a);
            dfs(A, i, N - 1, b);
        }
        for (int j = 0; j < N; ++j) {
            dfs(A, 0, j, a);
            dfs(A, M - 1, j, b);
        }
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                if (a[i][j] && b[i][j]) ans.push_back({i, j});
            }
        }
        return ans;
    }
};