forked from Sahana-Math/Section-G-SE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacific_atlantic_water_flow.cpp
48 lines (40 loc) · 1.76 KB
/
pacific_atlantic_water_flow.cpp
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
class Solution {
public:
/*if we look at this question in a completely opposite way, i.e water flowing
from ocean to land, if the current cell is greater than the prev cell, only
then water flows. */
void helper(vector<vector<int>>& heights, int i, int j, int prev, vector<vector<int>>& ocean) {
if(i < 0 or j < 0 or i >= heights.size() or j >= heights[0].size() or ocean[i][j] == 1 or heights[i][j] < prev)
return;
// if(ocean[i][j] == 1)
// return;
// if(height[i][j] < prev)
// return;
ocean[i][j] = 1;
helper(heights, i + 1, j, heights[i][j], ocean);
helper(heights, i - 1, j, heights[i][j], ocean);
helper(heights, i, j + 1, heights[i][j], ocean);
helper(heights, i, j - 1, heights[i][j], ocean);
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
vector<vector<int>> result;
vector<vector<int>> pacific(heights.size(), vector<int>(heights[0].size(), 0));
vector<vector<int>> atlantic(heights.size(), vector<int>(heights[0].size(), 0));
for(int i = 0; i < heights[0].size(); ++i) {
helper(heights, 0, i, -1, pacific);
helper(heights, heights.size() - 1, i, INT_MIN, atlantic);
}
for(int j = 0; j < heights.size(); ++j) {
helper(heights, j, 0, -1, pacific);
helper(heights, j, heights[0].size() - 1, INT_MIN, atlantic);
}
for(int i = 0; i < heights.size(); ++i) {
for(int j = 0; j < heights[0].size(); ++j) {
if(pacific[i][j] == 1 and atlantic[i][j] == 1) {
result.push_back({i, j});
}
}
}
return result;
}
};