Skip to content

Commit c5189fd

Browse files
committed
add pacific atlantic water flow solution
1 parent 239bfda commit c5189fd

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
6+
int[] dx = {1, -1, 0, 0};
7+
int[] dy = {0, 0, 1, -1};
8+
// boolean[][] visited;
9+
int width, height;
10+
11+
// 시간복잡도, 공간복잡도: O(m * n)
12+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
13+
height = heights.length;
14+
width = heights[0].length;
15+
16+
boolean[][] pacific = new boolean[height][width];
17+
boolean[][] atlantic = new boolean[height][width];
18+
19+
/**
20+
* 가장자리에 도달하는 경로를 일일이 탐색하는 것이 아닌
21+
가장자리부터 물이 흐를 수 있는 경로를 역으로 탐색
22+
*/
23+
24+
// height check
25+
for (int i = 0; i < height; i++) {
26+
dfs(i, 0, heights, pacific);
27+
dfs(i, width - 1, heights, atlantic);
28+
}
29+
30+
// width check
31+
for (int j = 0; j < width; j++) {
32+
dfs(0, j, heights, pacific);
33+
dfs(height - 1, j, heights, atlantic);
34+
}
35+
36+
List<List<Integer>> answer = new ArrayList<>();
37+
for (int i = 0; i < height; i++) {
38+
for (int j = 0; j < width; j++) {
39+
if (pacific[i][j] && atlantic[i][j]) {
40+
answer.add(List.of(i, j));
41+
}
42+
}
43+
}
44+
45+
return answer;
46+
}
47+
48+
private void dfs(int x, int y, int[][] heights, boolean[][] visited) {
49+
visited[x][y] = true;
50+
for (int i = 0; i < 4; i++) {
51+
int nx = x + dx[i];
52+
int ny = y + dy[i];
53+
if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
54+
if (!visited[nx][ny] && heights[nx][ny] >= heights[x][y]) {
55+
dfs(nx, ny, heights, visited);
56+
}
57+
}
58+
}
59+
}
60+
61+
// public List<List<Integer>> pacificAtlantic(int[][] heights) {
62+
// height = heights.length;
63+
// width = heights[0].length;
64+
// visited = new boolean[height][width];
65+
66+
// List<List<Integer>> answer = new ArrayList<>();
67+
// for (int i = 0; i < height; i++) {
68+
// for (int j = 0; j < width; j++) {
69+
// WaterFlow checkWaterFlow = dfs(i, j, new WaterFlow(), heights);
70+
// if (checkWaterFlow.crossBothOceans()) {
71+
// List<Integer> coordinate = List.of(i, j);
72+
// answer.add(coordinate);
73+
// }
74+
// }
75+
// }
76+
// return answer;
77+
// }
78+
79+
// private WaterFlow dfs(int x, int y, WaterFlow checkWaterFlow, int[][] heights) {
80+
// /**
81+
// 0, 0 | 0, 1 | 0, 2 | 0, 3 | 0, 4
82+
// 1, 0 | 2, 0 | 3, 0 | 4, 0
83+
// */
84+
// if (x == 0 || y == 0) {
85+
// checkWaterFlow.crossPacificFlow();
86+
// }
87+
// /**
88+
// 4, 0 | 4, 1 | 4, 2 | 4, 3 | 4, 4
89+
// 3, 4 | 2, 4 | 1, 4 | 0, 4
90+
// */
91+
// if (x == height - 1 || y == width - 1) {
92+
// checkWaterFlow.crossAtlanticFlow();
93+
// }
94+
95+
// if (checkWaterFlow.crossBothOceans()) {
96+
// return checkWaterFlow;
97+
// }
98+
99+
// visited[x][y] = true;
100+
101+
// for (int i = 0; i < 4; i++) {
102+
// int nx = x + dx[i];
103+
// int ny = y + dy[i];
104+
// if (nx >= 0 && nx < height && ny >= 0 && ny < width) {
105+
// if (!visited[nx][ny] && heights[nx][ny] <= heights[x][y]) {
106+
// dfs(nx, ny, checkWaterFlow, heights);
107+
// }
108+
// }
109+
// }
110+
// visited[x][y] = false;
111+
112+
// return checkWaterFlow;
113+
// }
114+
115+
// static class WaterFlow {
116+
117+
// private boolean pacificFlow;
118+
119+
// private boolean atlanticFlow;
120+
121+
// WaterFlow() {
122+
// pacificFlow = false;
123+
// atlanticFlow = false;
124+
// }
125+
126+
// public void crossPacificFlow() {
127+
// pacificFlow = true;
128+
// }
129+
130+
// public void crossAtlanticFlow() {
131+
// atlanticFlow = true;
132+
// }
133+
134+
// public boolean crossBothOceans() {
135+
// return pacificFlow && atlanticFlow;
136+
// }
137+
138+
// }
139+
}
140+

0 commit comments

Comments
 (0)