Skip to content

Commit eb1a955

Browse files
committed
✨ [417] Pacific Atlantic Water Flow
I THINK IT WORKS!!! - just need to convert map to list
1 parent 91de16a commit eb1a955

File tree

4 files changed

+132
-9
lines changed

4 files changed

+132
-9
lines changed

417/my_solution.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @param {number[][]} heights
3+
* @return {number[][]}
4+
*/
5+
const pacificAtlantic = (heights) => {
6+
console.log(heights);
7+
let result = [], resultMap = new Map();
8+
9+
const dfs = (start, prev, row, col, visitMap, oceanMap) => {
10+
if (row < 0 || col < 0 || row === heights.length || col === heights[0].length) {
11+
console.log("EXIT!!!")
12+
return;
13+
}
14+
15+
let curr = heights[row][col];
16+
if (prev < curr) {
17+
console.log(`>>> Cant proceed - prev:${prev} < curr:${curr}`)
18+
return;
19+
}
20+
21+
console.log(`------------------> start:${start}, prev:${prev}, row:${row}, col:${col}, n:${heights[row][col]}`)
22+
23+
// console.log("visitMap")
24+
// console.log(visitMap)
25+
console.log("oceanMap")
26+
console.log(oceanMap)
27+
28+
let coordinate = JSON.stringify([row, col]);
29+
if (visitMap.has(coordinate)) {
30+
console.log(`-------> Visited!`)
31+
return;
32+
}
33+
34+
if (
35+
(row === 0) ||
36+
(row === heights.length - 1) ||
37+
(col === 0) ||
38+
(col === heights[0].length - 1)
39+
) {
40+
41+
if (!oceanMap.get("p")) oceanMap.set("p", ((row === 0) || (col === 0)))
42+
if (!oceanMap.get("a")) oceanMap.set("a", ((row === heights.length - 1) || (col === heights[0].length - 1)))
43+
44+
console.log(`>> Found`)
45+
console.log(result)
46+
console.log(oceanMap)
47+
// return
48+
}
49+
50+
if (oceanMap.get("p") && oceanMap.get("a")) {
51+
console.log(">>>>>>>>>>>>>>> This is the ONE!!!")
52+
console.log(oceanMap);
53+
// result.push([row, col])
54+
resultMap.set(start, true)
55+
console.log(resultMap);
56+
57+
return
58+
}
59+
60+
61+
visitMap.set(coordinate, true)
62+
63+
console.log("visitMap")
64+
console.log(visitMap)
65+
66+
dfs(start, curr, row, col + 1, visitMap, oceanMap);
67+
dfs(start, curr, row + 1, col, visitMap, oceanMap);
68+
dfs(start, curr, row, col - 1, visitMap, oceanMap);
69+
dfs(start, curr, row - 1, col, visitMap, oceanMap);
70+
}
71+
72+
// dfs(Infinity, 0, 0, new Set())
73+
for (let row = 0; row < heights.length; row++) {
74+
for (let col = 0; col < heights[row].length; col++) {
75+
dfs(JSON.stringify([row, col]), Infinity, row, col, new Map(), new Map([["p", false], ["a", false]]), [])
76+
// console.log(heights[row][col])
77+
}
78+
}
79+
80+
console.log("result")
81+
console.log(result)
82+
83+
console.log("resultMap")
84+
console.log(resultMap)
85+
86+
return [[]]; // 2d array
87+
};
88+
89+
90+
// let x = pacificAtlantic([
91+
// [1, 2, 3],
92+
// [6, 5, 4],
93+
// [2, 7, 2],
94+
// ]);
95+
96+
let x = pacificAtlantic([
97+
[1, 2, 2, 3, 5],
98+
[3, 2, 3, 4, 4],
99+
[2, 4, 5, 3, 1],
100+
[6, 7, 1, 4, 5],
101+
[5, 1, 1, 2, 4]
102+
]);
103+
104+
// let x = pacificAtlantic([1]);
105+
console.log(x);

417/solution.js

Whitespace-only changes.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
- [374. Top K Frequent Elements](./374/)
5656
- [338. Counting Bits](./338/)
5757
- [394. Decode String](./394/)
58+
- [417. Pacific Atlantic Water Flow](./417/)
5859
- [424. Longest Repeating Character Replacement](./424/)
5960
- [435. Non-overlapping Intervals](./435/)
6061
- [647. Palindromic Substrings](./647/)
@@ -107,7 +108,7 @@ Batch create:
107108
NOTE: JS IS HERE
108109
-->
109110
```ssh
110-
chapter=1909 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
111+
chapter=417 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
111112
```
112113
> then you can use `x` for quick debug.
113114

test.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,30 @@
4848
//
4949
//
5050

51-
let o = Array.from({ length: 3 }, () => []);
52-
o[2] = [1, 3, 4]
53-
console.log(o)
51+
// Fill
52+
// let o = Array.from({ length: 3 }, () => []);
53+
// o[2] = [1, 3, 4]
54+
// console.log(o)
55+
//
56+
// let x = Array(3).fill([]);
57+
// x[1].push(2)
58+
// console.log(x)
59+
//
60+
// const g = Array(3).fill().map(() => []);
61+
// console.log(g)
62+
5463

55-
let x = Array(3).fill([]);
56-
x[1].push(2)
57-
console.log(x)
64+
// Set
65+
// let set = new Set();
66+
// let x = [2, 2]
67+
// set.add([1, 2])
68+
// set.add([0, 3])
69+
// set.add(x)
70+
// console.log(set)
71+
// console.log(set.has([0, 2]))
72+
// console.log(set.has([0, 3]))
73+
// console.log(set.has([1, 2]))
74+
// console.log(set.has(x))
5875

59-
const g = Array(3).fill().map(() => []);
60-
console.log(g)
76+
// Map
77+
console.log(new Map([["key", "value"], ["rrsrsr", true]]))

0 commit comments

Comments
 (0)