-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path25. Pacific Atlantic Water Flow
120 lines (103 loc) · 3.46 KB
/
25. Pacific Atlantic Water Flow
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//BruteForce
class Solution {
int m,n;
int[][] matrix;
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
List<List<Integer>> result=new ArrayList<>();
if(matrix.length==0 || matrix[0].length==0) return result;
m=matrix.length;
n=matrix[0].length;
this.matrix=matrix;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
//If both flags are true, add in result
if(reachPacific(i,j) && reachAtlantic(i,j)){
result.add(Arrays.asList(i,j));
}
}
}
return result;
}
boolean reachPacific(int r,int c){
if(r==0 || c==0) return true;
if(matrix[r][c]==-1) return false;
int val=matrix[r][c];
matrix[r][c]=-1;
boolean flag=false;
//Call DFS in all 4 Directions
if(matrix[r-1][c]<=val){
flag=reachPacific(r-1,c);
}
if(!flag && matrix[r][c-1]<=val){
flag = reachPacific(r,c-1);
}
if(!flag && c+1<n && matrix[r][c+1]<=val){
flag = reachPacific(r,c+1);
}
if(!flag && r+1<m && matrix[r+1][c]<=val){
flag = reachPacific(r+1,c);
}
matrix[r][c]=val;
return flag;
}
boolean reachAtlantic(int r,int c){
if(r==m-1 || c==n-1) return true;
if(matrix[r][c]==-1) return false;
int val=matrix[r][c];
matrix[r][c]=-1;
boolean flag=false;
//Call DFS in all 4 Directions
if(matrix[r+1][c]<=val){
flag=reachAtlantic(r+1,c);
}
if(!flag && matrix[r][c+1]<=val){
flag = reachAtlantic(r,c+1);
}
if(!flag && c-1>=0 && matrix[r][c-1]<=val){
flag = reachAtlantic(r,c-1);
}
if(!flag && r-1>=0 && matrix[r-1][c]<=val){
flag = reachAtlantic(r-1,c);
}
matrix[r][c]=val;
return flag;
}
}
//Optimized
class Solution {
int dir[][] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
List<List<Integer>> res = new ArrayList<>();
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return res;
int row = matrix.length, col = matrix[0].length;
boolean[][] pacific = new boolean[row][col];
boolean[][] atlantic = new boolean[row][col];
//DFS
for(int i = 0; i < col; i++){
dfs(matrix, 0, i, Integer.MIN_VALUE, pacific);
dfs(matrix, row-1, i, Integer.MIN_VALUE, atlantic);
}
for(int i = 0; i < row; i++){
dfs(matrix, i, 0, Integer.MIN_VALUE, pacific);
dfs(matrix, i, col-1, Integer.MIN_VALUE, atlantic);
}
//preparing the result
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++) {
if(pacific[i][j] && atlantic[i][j]) {
res.add(Arrays.asList(i,j));
}
}
}
return res;
}
public void dfs(int[][] matrix, int i, int j, int prev, boolean[][] ocean){
if(i < 0 || i >= ocean.length || j < 0 || j >= ocean[0].length) return;
if(matrix[i][j] < prev || ocean[i][j]) return;
ocean[i][j] = true;
for(int[] d : dir){
dfs(matrix, i+d[0], j+d[1], matrix[i][j], ocean);
}
}
}