Skip to content

Commit 1b3b88e

Browse files
add: Pacific Atlantic Water Flow
1 parent aed3ca8 commit 1b3b88e

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

clone-graph/Solution.java renamed to clone-graph/YoungSeok-Choi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ public void print(Node node) {
8383
print(adj);
8484
}
8585
}
86-
}
86+
}

linked-list-cycle/Solution.java renamed to linked-list-cycle/YoungSeok-Choi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ public boolean hasCycle(ListNode head) {
6464
return false;
6565
}
6666
}
67-
}
67+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
// TC -> O(nm^2) 일 것으로 예상..
6+
class Solution {
7+
8+
public boolean[][] visit;
9+
public List<List<Integer>> res = new ArrayList<>();
10+
public int w;
11+
public int h;
12+
13+
public int[] dx = { 0, 1, 0, -1 };
14+
public int[] dy = { 1, 0, -1, 0 };
15+
16+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
17+
w = heights.length;
18+
h = heights[0].length;
19+
20+
if (w == h && w == 1) {
21+
res.add(Arrays.asList(0, 0));
22+
return res;
23+
}
24+
25+
for (int i = 0; i < w; i++) {
26+
for (int j = 0; j < h; j++) {
27+
28+
boolean isTrue = false;
29+
visit = new boolean[w][h];
30+
31+
isTrue |= dfsP(i, j, heights[i][j], heights);
32+
33+
visit = new boolean[w][h];
34+
35+
isTrue &= dfsA(i, j, heights[i][j], heights);
36+
37+
if (isTrue) {
38+
res.add(Arrays.asList(i, j));
39+
}
40+
}
41+
}
42+
43+
return res;
44+
}
45+
46+
public boolean dfsP(int x, int y, int prev, int[][] map) {
47+
if (x < 0 || y < 0) {
48+
return true;
49+
}
50+
51+
if (x >= w || y >= h) {
52+
return false;
53+
}
54+
55+
if (visit[x][y]) {
56+
return false;
57+
}
58+
59+
if (prev < map[x][y]) {
60+
return false;
61+
}
62+
63+
visit[x][y] = true;
64+
65+
boolean isTrue = false;
66+
for (int i = 0; i < 4; i++) {
67+
int nx = x + dx[i];
68+
int ny = y + dy[i];
69+
70+
isTrue |= dfsP(nx, ny, map[x][y], map);
71+
if (isTrue) {
72+
return true;
73+
}
74+
}
75+
76+
return isTrue;
77+
}
78+
79+
public boolean dfsA(int x, int y, int prev, int[][] map) {
80+
if (x < 0 || y < 0) {
81+
return false;
82+
}
83+
84+
if (x >= w || y >= h) {
85+
return true;
86+
}
87+
88+
if (visit[x][y]) {
89+
return false;
90+
}
91+
92+
if (prev < map[x][y]) {
93+
return false;
94+
}
95+
96+
visit[x][y] = true;
97+
98+
boolean isTrue = false;
99+
for (int i = 0; i < 4; i++) {
100+
int nx = x + dx[i];
101+
int ny = y + dy[i];
102+
103+
isTrue |= dfsA(nx, ny, map[x][y], map);
104+
105+
if (isTrue) {
106+
return true;
107+
}
108+
109+
}
110+
111+
return isTrue;
112+
}
113+
}

0 commit comments

Comments
 (0)