-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacific Atlantic Water Flow.cpp
63 lines (47 loc) · 1.68 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
vector<vector<int>>ans;
if(matrix.size()<1)return ans;
vector<vector<int>>pacific(matrix.size(),vector<int>(matrix[0].size(),0));
vector<vector<int>>atlantic(matrix.size(),vector<int>(matrix[0].size(),0));
for(int col=0;col<matrix[0].size();col++)
{
fnc(matrix,0,col,INT_MIN,pacific);
fnc(matrix,matrix.size()-1,col,INT_MIN,atlantic);
}
for(int row = 0;row<matrix.size();row++)
{
fnc(matrix,row,0,INT_MIN,pacific);
fnc(matrix,row,matrix[0].size()-1,INT_MIN,atlantic);
}
for(int i=0;i<matrix.size();i++)
{
for(int j=0;j<matrix[0].size();j++)
{
if(pacific[i][j]==1 && atlantic[i][j]==1)
{
vector<int>v(2);
v[0]=i;
v[1]=j;
ans.push_back(v);
}
}
}
return ans;
}
void fnc(vector<vector<int>>& matrix,int i, int j,int prev,vector<vector<int>>& ocean)
{
if(i<0 || j<0 || i>=matrix.size() || j>=matrix[0].size())
return;
if(ocean[i][j]==1)
return;
if(matrix[i][j]<prev)
return;
ocean[i][j]=1;
fnc(matrix,i+1,j,matrix[i][j],ocean);
fnc(matrix,i-1,j,matrix[i][j],ocean);
fnc(matrix,i,j+1,matrix[i][j],ocean);
fnc(matrix,i,j-1,matrix[i][j],ocean);
}
};