2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
5
- /**Given the following 5x5 matrix:
5
+ /**
6
+ * 417. Pacific Atlantic Water Flow
7
+ *
8
+ * Given the following 5x5 matrix:
6
9
7
10
Pacific ~ ~ ~ ~ ~
8
11
~ 1 2 2 3 (5) *
17
20
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).*/
18
21
19
22
public class _417 {
20
- //looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
21
-
22
- /**
23
- * One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.
24
- */
25
- public List <int []> pacificAtlantic (int [][] matrix ) {
26
-
27
- List <int []> result = new ArrayList ();
28
- if (matrix == null || matrix .length == 0 || matrix [0 ].length == 0 ) {
29
- return result ;
30
- }
23
+ public static class Solution1 {
24
+ /**
25
+ * Credit: looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
26
+ * <p>
27
+ * One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.
28
+ */
29
+ public List <int []> pacificAtlantic (int [][] matrix ) {
30
+
31
+ List <int []> result = new ArrayList ();
32
+ if (matrix == null || matrix .length == 0 || matrix [0 ].length == 0 ) {
33
+ return result ;
34
+ }
31
35
32
- int m = matrix .length ;
33
- int n = matrix [0 ].length ;
34
- boolean [][] pacific = new boolean [m ][n ];
35
- boolean [][] atlantic = new boolean [m ][n ];
36
+ int m = matrix .length ;
37
+ int n = matrix [0 ].length ;
38
+ boolean [][] pacific = new boolean [m ][n ];
39
+ boolean [][] atlantic = new boolean [m ][n ];
36
40
37
- for (int i = 0 ; i < m ; i ++) {
38
- dfs (matrix , pacific , Integer .MIN_VALUE , i , 0 );
39
- dfs (matrix , atlantic , Integer .MIN_VALUE , i , n - 1 );
40
- }
41
+ for (int i = 0 ; i < m ; i ++) {
42
+ dfs (matrix , pacific , Integer .MIN_VALUE , i , 0 );
43
+ dfs (matrix , atlantic , Integer .MIN_VALUE , i , n - 1 );
44
+ }
41
45
42
- for (int i = 0 ; i < n ; i ++) {
43
- dfs (matrix , pacific , Integer .MIN_VALUE , 0 , i );
44
- dfs (matrix , atlantic , Integer .MIN_VALUE , m - 1 , i );
45
- }
46
+ for (int i = 0 ; i < n ; i ++) {
47
+ dfs (matrix , pacific , Integer .MIN_VALUE , 0 , i );
48
+ dfs (matrix , atlantic , Integer .MIN_VALUE , m - 1 , i );
49
+ }
46
50
47
- for (int i = 0 ; i < m ; i ++) {
48
- for (int j = 0 ; j < n ; j ++) {
49
- if (pacific [i ][j ] && atlantic [i ][j ]) {
50
- result .add (new int []{i , j });
51
+ for (int i = 0 ; i < m ; i ++) {
52
+ for (int j = 0 ; j < n ; j ++) {
53
+ if (pacific [i ][j ] && atlantic [i ][j ]) {
54
+ result .add (new int []{i , j });
55
+ }
51
56
}
52
57
}
53
- }
54
-
55
- return result ;
56
- }
57
58
58
- void dfs (int [][] matrix , boolean [][] visited , int height , int x , int y ) {
59
- int m = matrix .length ;
60
- int n = matrix [0 ].length ;
61
- if (x < 0 || y < 0 || x >= m || y >= n || matrix [x ][y ] < height || visited [x ][y ]) {
62
- return ;
59
+ return result ;
63
60
}
64
- visited [x ][y ] = true ;
65
- dfs (matrix , visited , matrix [x ][y ], x + 1 , y );
66
- dfs (matrix , visited , matrix [x ][y ], x , y + 1 );
67
- dfs (matrix , visited , matrix [x ][y ], x - 1 , y );
68
- dfs (matrix , visited , matrix [x ][y ], x , y - 1 );
69
- }
70
61
71
- public static void main (String ... args ) {
72
- _417 test = new _417 ();
73
- // int[][] matrix = new int[][]{
74
- // {1,2,2,3,5},
75
- // {3,2,3,4,4},
76
- // {2,4,5,3,1},
77
- // {6,7,1,4,5},
78
- // {5,1,1,2,4},
79
- // };
80
-
81
- // int[][] matrix = new int[][]{//this one is correct
82
- // {3,5},
83
- // {4,4},
84
- // };
85
-
86
- // int[][] matrix = new int[][]{//this one is correct
87
- // {2,3,5},
88
- // {3,4,4},
89
- // };
90
-
91
- int [][] matrix = new int [][]{
92
- {2 , 3 , 5 },
93
- {3 , 4 , 4 },
94
- {5 , 3 , 1 },
95
- };
96
- List <int []> result = test .pacificAtlantic (matrix );
97
- for (int [] point : result ) {
98
- System .out .println (point [0 ] + "\t " + point [1 ]);
62
+ void dfs (int [][] matrix , boolean [][] visited , int height , int x , int y ) {
63
+ int m = matrix .length ;
64
+ int n = matrix [0 ].length ;
65
+ if (x < 0 || y < 0 || x >= m || y >= n || matrix [x ][y ] < height || visited [x ][y ]) {
66
+ return ;
67
+ }
68
+ visited [x ][y ] = true ;
69
+ dfs (matrix , visited , matrix [x ][y ], x + 1 , y );
70
+ dfs (matrix , visited , matrix [x ][y ], x , y + 1 );
71
+ dfs (matrix , visited , matrix [x ][y ], x - 1 , y );
72
+ dfs (matrix , visited , matrix [x ][y ], x , y - 1 );
99
73
}
100
74
}
101
75
102
- }
76
+ }
0 commit comments