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