Skip to content

Commit 388e968

Browse files
committed
😢 [417] PASSED!!! I DID IT!!!!!!!!
1 parent eb1a955 commit 388e968

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

417/my_solution.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55
const pacificAtlantic = (heights) => {
66
console.log(heights);
7-
let result = [], resultMap = new Map();
7+
8+
if (heights.length === 1 && heights[0].length === 1) return [[0, 0]];
9+
10+
let resultMap = new Map();
811

912
const dfs = (start, prev, row, col, visitMap, oceanMap) => {
1013
if (row < 0 || col < 0 || row === heights.length || col === heights[0].length) {
@@ -42,15 +45,12 @@ const pacificAtlantic = (heights) => {
4245
if (!oceanMap.get("a")) oceanMap.set("a", ((row === heights.length - 1) || (col === heights[0].length - 1)))
4346

4447
console.log(`>> Found`)
45-
console.log(result)
4648
console.log(oceanMap)
47-
// return
4849
}
4950

5051
if (oceanMap.get("p") && oceanMap.get("a")) {
5152
console.log(">>>>>>>>>>>>>>> This is the ONE!!!")
5253
console.log(oceanMap);
53-
// result.push([row, col])
5454
resultMap.set(start, true)
5555
console.log(resultMap);
5656

@@ -69,21 +69,17 @@ const pacificAtlantic = (heights) => {
6969
dfs(start, curr, row - 1, col, visitMap, oceanMap);
7070
}
7171

72-
// dfs(Infinity, 0, 0, new Set())
7372
for (let row = 0; row < heights.length; row++) {
7473
for (let col = 0; col < heights[row].length; col++) {
7574
dfs(JSON.stringify([row, col]), Infinity, row, col, new Map(), new Map([["p", false], ["a", false]]), [])
76-
// console.log(heights[row][col])
7775
}
7876
}
7977

80-
console.log("result")
81-
console.log(result)
82-
8378
console.log("resultMap")
8479
console.log(resultMap)
8580

86-
return [[]]; // 2d array
81+
82+
return Array.from(resultMap.entries(), entry => JSON.parse(entry[0])); // 2d array
8783
};
8884

8985

@@ -93,13 +89,14 @@ const pacificAtlantic = (heights) => {
9389
// [2, 7, 2],
9490
// ]);
9591

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-
]);
92+
// let x = pacificAtlantic([
93+
// [1, 2, 2, 3, 5],
94+
// [3, 2, 3, 4, 4],
95+
// [2, 4, 5, 3, 1],
96+
// [6, 7, 1, 4, 5],
97+
// [5, 1, 1, 2, 4]
98+
// ]);
10399

104-
// let x = pacificAtlantic([1]);
100+
let x = pacificAtlantic([1]);
101+
console.log("Result");
105102
console.log(x);

417/solution.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @param {number[][]} heights
3+
* @return {number[][]}
4+
*/
5+
const pacificAtlantic = (heights) => {
6+
console.log(heights);
7+
8+
if (heights.length === 1 && heights[0].length === 1) return [[0, 0]];
9+
10+
let resultMap = new Map();
11+
12+
const dfs = (start, prev, row, col, visitMap, oceanMap) => {
13+
if (row < 0 || col < 0 || row === heights.length || col === heights[0].length) return;
14+
15+
let curr = heights[row][col];
16+
17+
if (prev < curr) return;
18+
19+
let coordinate = JSON.stringify([row, col]);
20+
21+
if (visitMap.has(coordinate)) return;
22+
23+
if (
24+
(row === 0) ||
25+
(row === heights.length - 1) ||
26+
(col === 0) ||
27+
(col === heights[0].length - 1)
28+
) {
29+
if (!oceanMap.get("p")) oceanMap.set("p", ((row === 0) || (col === 0)))
30+
if (!oceanMap.get("a")) oceanMap.set("a", ((row === heights.length - 1) || (col === heights[0].length - 1)))
31+
}
32+
33+
if (oceanMap.get("p") && oceanMap.get("a")) {
34+
resultMap.set(start, true)
35+
return
36+
}
37+
38+
visitMap.set(coordinate, true)
39+
40+
dfs(start, curr, row, col + 1, visitMap, oceanMap);
41+
dfs(start, curr, row + 1, col, visitMap, oceanMap);
42+
dfs(start, curr, row, col - 1, visitMap, oceanMap);
43+
dfs(start, curr, row - 1, col, visitMap, oceanMap);
44+
}
45+
46+
for (let row = 0; row < heights.length; row++) {
47+
for (let col = 0; col < heights[row].length; col++) {
48+
dfs(JSON.stringify([row, col]), Infinity, row, col, new Map(), new Map([["p", false], ["a", false]]), [])
49+
}
50+
}
51+
52+
return Array.from(resultMap.entries(), entry => JSON.parse(entry[0]));
53+
};

0 commit comments

Comments
 (0)