Skip to content

Commit 5d0a623

Browse files
committed
Feat: 417. Pacific Atlantic Water Flow
1 parent cd17daa commit 5d0a623

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* https://leetcode.com/problems/pacific-atlantic-water-flow
3+
* T.C. O((m * n)^2)
4+
* S.C. O(m * n)
5+
*/
6+
function pacificAtlantic(heights: number[][]): number[][] {
7+
const dir = [
8+
[0, 1],
9+
[0, -1],
10+
[1, 0],
11+
[-1, 0],
12+
];
13+
14+
function pacificDfs(row: number, col: number, visited: Set<string>) {
15+
const key = `${row},${col}`;
16+
if (visited.has(key)) return;
17+
visited.add(key);
18+
19+
if (row === 0 || col === 0) {
20+
// left or top
21+
return true;
22+
}
23+
24+
for (let [r, c] of dir) {
25+
if (row + r < 0 || row + r >= heights.length) continue;
26+
if (col + c < 0 || col + c >= heights[0].length) continue;
27+
if (heights[row][col] < heights[row + r][col + c]) continue;
28+
if (pacificDfs(row + r, col + c, visited)) return true;
29+
}
30+
return false;
31+
}
32+
33+
function atlanticDfs(row: number, col: number, visited: Set<string>) {
34+
const key = `${row},${col}`;
35+
if (visited.has(key)) return;
36+
visited.add(key);
37+
38+
if (row === heights.length - 1 || col === heights[0].length - 1) {
39+
// right or bottom
40+
return true;
41+
}
42+
43+
for (let [r, c] of dir) {
44+
if (row + r < 0 || row + r >= heights.length) continue;
45+
if (col + c < 0 || col + c >= heights[0].length) continue;
46+
if (heights[row][col] < heights[row + r][col + c]) continue;
47+
if (atlanticDfs(row + r, col + c, visited)) return true;
48+
}
49+
return false;
50+
}
51+
52+
const result: number[][] = [];
53+
for (let i = 0; i < heights.length; i++) {
54+
for (let j = 0; j < heights[0].length; j++) {
55+
if (
56+
pacificDfs(i, j, new Set<string>()) &&
57+
atlanticDfs(i, j, new Set<string>())
58+
) {
59+
result.push([i, j]);
60+
}
61+
}
62+
}
63+
64+
return result;
65+
}
66+
67+
/**
68+
* T.C. O(m * n)
69+
* S.C. O(m * n)
70+
*/
71+
function pacificAtlantic(heights: number[][]): number[][] {
72+
const pacific: Set<string> = new Set();
73+
const atlantic: Set<string> = new Set();
74+
75+
const dir = [
76+
[0, 1],
77+
[0, -1],
78+
[1, 0],
79+
[-1, 0],
80+
];
81+
82+
function dfs(row: number, col: number, visited: Set<string>) {
83+
const key = `${row},${col}`;
84+
if (visited.has(key)) return;
85+
visited.add(key);
86+
87+
for (let [r, c] of dir) {
88+
if (row + r < 0 || row + r >= heights.length) continue;
89+
if (col + c < 0 || col + c >= heights[0].length) continue;
90+
if (heights[row][col] > heights[row + r][col + c]) continue;
91+
dfs(row + r, col + c, visited);
92+
}
93+
}
94+
95+
for (let i = 0; i < heights.length; i++) {
96+
dfs(i, 0, pacific);
97+
dfs(i, heights[0].length - 1, atlantic);
98+
}
99+
100+
for (let i = 0; i < heights[0].length; i++) {
101+
dfs(0, i, pacific);
102+
dfs(heights.length - 1, i, atlantic);
103+
}
104+
105+
const result: number[][] = [];
106+
107+
for (const p of pacific) {
108+
if (atlantic.has(p)) {
109+
const [row, col] = p.split(',').map(Number);
110+
result.push([row, col]);
111+
}
112+
}
113+
114+
return result;
115+
}

0 commit comments

Comments
 (0)