Skip to content

Commit ac85f2d

Browse files
pacific and atlantic water flow
1 parent 505ea2b commit ac85f2d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
/**Given the following 5x5 matrix:
6+
7+
Pacific ~ ~ ~ ~ ~
8+
~ 1 2 2 3 (5) *
9+
~ 3 2 3 (4) (4) *
10+
~ 2 4 (5) 3 1 *
11+
~ (6) (7) 1 4 5 *
12+
~ (5) 1 1 2 4 *
13+
* * * * * Atlantic
14+
15+
Return:
16+
17+
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).*/
18+
19+
public class PacificAtlanticWaterFlow {
20+
//looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
21+
22+
/**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.*/
23+
public List<int[]> pacificAtlantic(int[][] matrix) {
24+
25+
List<int[]> result = new ArrayList();
26+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return result;
27+
28+
int m = matrix.length, n = matrix[0].length;
29+
boolean[][] pacific = new boolean[m][n];
30+
boolean[][] atlantic = new boolean[m][n];
31+
32+
for(int i = 0; i < m; i++){
33+
dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);
34+
dfs(matrix, atlantic, Integer.MIN_VALUE, i, n-1);
35+
}
36+
37+
for(int i = 0; i < n; i++){
38+
dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);
39+
dfs(matrix, atlantic, Integer.MIN_VALUE, m-1, i);
40+
}
41+
42+
for(int i = 0; i < m; i++){
43+
for(int j = 0; j < n; j++){
44+
if(pacific[i][j] && atlantic[i][j]){
45+
result.add(new int[]{i, j});
46+
}
47+
}
48+
}
49+
50+
return result;
51+
}
52+
53+
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y){
54+
int m = matrix.length, n = matrix[0].length;
55+
if(x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) return;
56+
visited[x][y] = true;
57+
dfs(matrix, visited, matrix[x][y], x+1, y);
58+
dfs(matrix, visited, matrix[x][y], x, y+1);
59+
dfs(matrix, visited, matrix[x][y], x-1, y);
60+
dfs(matrix, visited, matrix[x][y], x, y-1);
61+
}
62+
63+
public static void main(String...args){
64+
PacificAtlanticWaterFlow test = new PacificAtlanticWaterFlow();
65+
// int[][] matrix = new int[][]{
66+
// {1,2,2,3,5},
67+
// {3,2,3,4,4},
68+
// {2,4,5,3,1},
69+
// {6,7,1,4,5},
70+
// {5,1,1,2,4},
71+
// };
72+
73+
// int[][] matrix = new int[][]{//this one is correct
74+
// {3,5},
75+
// {4,4},
76+
// };
77+
78+
// int[][] matrix = new int[][]{//this one is correct
79+
// {2,3,5},
80+
// {3,4,4},
81+
// };
82+
83+
int[][] matrix = new int[][]{
84+
{2,3,5},
85+
{3,4,4},
86+
{5,3,1},
87+
};
88+
List<int[]> result = test.pacificAtlantic(matrix);
89+
for(int[] point : result){
90+
System.out.println(point[0] + "\t" + point[1]);
91+
}
92+
}
93+
94+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
44
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1) | Easy|
55
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
6+
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS
67
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../../blob/master/EASY/src/easy/AddStrings.java)| O(n)|O(1) | Easy|
78
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../../blob/master/EASY/src/easy/FizzBuzz.java)| O(n)|O(1) | Easy|
89
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/)|[Solution](../../blob/master/EASY/src/easy/SumofLeftLeaves.java)| O(n)|O(h) | Easy|

0 commit comments

Comments
 (0)