Pacific Atlantic Water Flow Problem


Description

LeetCode Problem 417.

There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is less than or equal to the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [r_i, c_i] denotes that rain water can flow from cell (r_i, c_i) to both the Pacific and Atlantic oceans.

Example 1:

1
2
Input: heights = [[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]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]

Example 2:

1
2
Input: heights = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]

Constraints:

  • m == heights.length
  • n == heights[r].length
  • 1 <= m, n <= 200
  • 0 <= heights[r][c] <= 10^5


Sample C++ Code

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
class Solution {
public:
    void bfs(vector<vector<int>>& matrix, map<pair<int,int>, int>& ocean, 
            queue<pair<int, int>>& oceanQ) {
        int row = matrix.size();
        int col = matrix[0].size();
        
        int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
        while (!oceanQ.empty()) {
            pair<int, int> curr = oceanQ.front();
            oceanQ.pop();
            for (int i = 0; i < 4; i ++) {
                int new_r = curr.first + dir[i][0];
                int new_c = curr.second + dir[i][1];
                if ((new_r >= 0) && (new_r < row) && (new_c >= 0) && (new_c < col)) {
                    if (matrix[new_r][new_c] >= matrix[curr.first][curr.second]) {
                        if (ocean.find({new_r, new_c}) == ocean.end()) {
                            oceanQ.push({new_r, new_c});
                            ocean[{new_r, new_c}] = 1;
                        }
                    }
                }
            }
        }
        
        return;
    }
    
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        map<pair<int,int>, int> pacific, atlantic;
        
        if (matrix.size() == 0)
            return matrix;
        int row = matrix.size();
        int col = matrix[0].size();
        
        queue<pair<int, int>> pacificQ, atlanticQ;
        
        for (int i = 0; i < row; i ++) {
            pacificQ.push({i, 0});
            pacific[{i, 0}] = 1;
            atlanticQ.push({i, col-1});
            atlantic[{i, col-1}] = 1;
        }
        for (int j = 0; j < col; j ++) {
            pacificQ.push({0, j});
            pacific[{0, j}] = 1;
            atlanticQ.push({row-1, j});
            atlantic[{row-1, j}] = 1;
        }
        
        bfs(matrix, pacific, pacificQ);
        bfs(matrix, atlantic, atlanticQ);
        
        vector<vector<int>> ans;
        for (map<pair<int, int>, int>::iterator mit = pacific.begin(); 
            mit != pacific.end(); mit ++) {
            if (atlantic.find(mit->first) != atlantic.end()) {
                ans.push_back({mit->first.first, mit->first.second});
            }
        }
        return ans;
    }
};




Related Posts

Snakes And Ladders Problem

LeetCode 909. You are given an n x n integer...

Rotting Oranges Problem

LeetCode 994. You are given an m x n grid...

Numbers With Same Consecutive Differences Problem

LeetCode 967. Return all non-negative integers of length n such...

K-Similar Strings Problem

LeetCode 854. Strings s1 and s2 are k-similar (for some...