-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-061-islandCount.js
105 lines (82 loc) · 2.61 KB
/
structy-061-islandCount.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
103
104
105
// https://structy.net/problems/island-count
// p: grid
// r: num
// alvin's nested for loop + dfs
const islandCount = (grid) => {
const m = grid.length;
const n = grid[0].length;
const visited = new Set();
let count = 0;
for (let y = 0; y < m; y++) {
for (let x = 0; x < n; x++) {
if (explore(grid, y, x, visited)) {
count++;
}
}
}
return count;
};
const explore = (grid, y, x, visited) => {
const boundY = 0 <= y && y < grid.length;
const boundX = 0 <= x && x < grid[0].length;
if (!boundY || !boundX) return false;
if (grid[y][x] === "W") return false;
const currentNode = y + "," + x;
if (visited.has(currentNode)) return false;
visited.add(currentNode);
explore(grid, y - 1, x, visited);
explore(grid, y + 1, x, visited);
explore(grid, y, x - 1, visited);
explore(grid, y, x + 1, visited);
return true;
};
const grid = [
["W", "L", "W", "W", "W"],
["W", "L", "W", "W", "W"],
["W", "W", "W", "L", "W"],
["W", "W", "L", "L", "W"],
["L", "W", "W", "L", "L"],
["L", "L", "W", "W", "W"],
];
console.log(islandCount(grid)); // -> 3
// working solution but the code is bad:
// const islandCount = (grid) => {
// const m = grid.length;
// const n = grid[0].length;
// const visited = new Set();
// let count = 0;
// for (let y = 0; y < m; y++) {
// for (let x = 0; x < m; x++) {
// if (grid[y][x] === "L") {
// if (!visited.has(`${y}, ${x}`)) {
// visited.add(`${y}, ${x}`);
// const stack = [[y, x]];
// count++;
// while (stack.length > 0) {
// console.log(stack);
// const [i, j] = stack.pop();
// !visited.has(`${i + 1}, ${j}`) &&
// i + 1 < m &&
// grid[i + 1][j] === "L" &&
// visited.add(`${i + 1}, ${j}`) &&
// stack.push([i + 1, j]);
// !visited.has(`${i}, ${j - 1}`) &&
// j - 1 > 0 &&
// grid[i][j - 1] === "L" &&
// visited.add(`${i}, ${j - 1}`) &&
// stack.push([i, j - 1]);
// !visited.has(`${i}, ${j + 1}`) &&
// j + 1 < n &&
// grid[i][j + 1] === "L" &&
// visited.add(`${i}, ${j + 1}`) &&
// stack.push([i, j + 1]);
// }
// console.log(visited);
// } else {
// continue;
// }
// }
// }
// }
// return count;
// };