Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3eaf4dc

Browse files
committedMay 13, 2023
✨ [q200] Online solution
1 parent c7db01f commit 3eaf4dc

File tree

2 files changed

+102
-43
lines changed

2 files changed

+102
-43
lines changed
 

‎200/my_solution.js

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,21 @@ const processMap = (map, pointer_map, order_list, max_key) => {
5454

5555
let curr_list = [];
5656

57-
console.log("~> top")
58-
top = findNeighbours(first, { ...map, ...pointer_map }, 'y', 0, -1, [])
59-
// right = x + 1
60-
console.log("~> right")
61-
// console.log(top)
62-
right = findNeighbours(first, { ...map, ...pointer_map }, 'x', 1, 0, [])
63-
// bottom = y + 1
64-
console.log("~> bottom")
65-
// console.log([...top, ...right])
66-
bottom = findNeighbours(first, { ...map, ...pointer_map }, 'y', 0, 1, [])
67-
// left = x - 1
68-
console.log("~> left")
69-
// console.log([...top, ...right, ...bottom])
70-
left = findNeighbours(first, { ...map, ...pointer_map }, 'x', -1, 0, [])
57+
let expanded_list = expandFromNode(map, pointer_map);
58+
// console.log("~> top")
59+
// top = findNeighbours(first, { ...map, ...pointer_map }, 'y', 0, -1, [])
60+
// // right = x + 1
61+
// console.log("~> right")
62+
// // console.log(top)
63+
// right = findNeighbours(first, { ...map, ...pointer_map }, 'x', 1, 0, [])
64+
// // bottom = y + 1
65+
// console.log("~> bottom")
66+
// // console.log([...top, ...right])
67+
// bottom = findNeighbours(first, { ...map, ...pointer_map }, 'y', 0, 1, [])
68+
// // left = x - 1
69+
// console.log("~> left")
70+
// // console.log([...top, ...right, ...bottom])
71+
// left = findNeighbours(first, { ...map, ...pointer_map }, 'x', -1, 0, [])
7172

7273
console.log("pointer_map")
7374
console.log(pointer_map)
@@ -81,7 +82,7 @@ const processMap = (map, pointer_map, order_list, max_key) => {
8182
let new_max_key = max_key + 1;
8283

8384
console.log(`~> new_max_key: ${new_max_key}`)
84-
let expanded_list = [...top, ...right, ...bottom, ...left];
85+
// let expanded_list = [...top, ...right, ...bottom, ...left];
8586
// let next_first = map[expanded_list.sort()[0]] || map[Object.keys(map)[0]];
8687
// let next_first = map[Object.keys(map)[0]];
8788
// let next_first = map[expanded_list.sort((a, b) => a - b)[0]] || map[Object.keys(map)[0]];
@@ -175,6 +176,26 @@ const processMap = (map, pointer_map, order_list, max_key) => {
175176
return processMap(map, pointer_map, order_list, max_key);
176177
}
177178

179+
const expandFromNode = (map, pointer_map) => {
180+
let node = map[Object.keys(map)[0]];
181+
182+
console.log("~> top")
183+
top = findNeighbours(node, { ...map, ...pointer_map }, 'y', 0, -1, [])
184+
// right = x + 1
185+
console.log("~> right")
186+
// console.log(top)
187+
right = findNeighbours(node, { ...map, ...pointer_map }, 'x', 1, 0, [])
188+
// bottom = y + 1
189+
console.log("~> bottom")
190+
// console.log([...top, ...right])
191+
bottom = findNeighbours(node, { ...map, ...pointer_map }, 'y', 0, 1, [])
192+
// left = x - 1
193+
console.log("~> left")
194+
// console.log([...top, ...right, ...bottom])
195+
left = findNeighbours(node, { ...map, ...pointer_map }, 'x', -1, 0, [])
196+
197+
return [...top, ...right, ...bottom, ...left];
198+
}
178199
// Find neighbours going outwards like a +, neighbours will include the current node
179200
const findNeighbours = (current_node, map, attr, x_operator, y_operator, neighbours) => {
180201
// console.log("findNeighbours -> map")
@@ -211,12 +232,12 @@ const findNeighbours = (current_node, map, attr, x_operator, y_operator, neighbo
211232
return findNeighbours(current_node, map, attr, x_operator, y_operator, neighbours);
212233
}
213234

214-
// numIslands([
215-
// ["1", "1", "0", "0", "0"],
216-
// ["1", "1", "0", "0", "0"],
217-
// ["1", "0", "1", "0", "0"],
218-
// ["1", "0", "0", "1", "1"]
219-
// ]);
235+
numIslands([
236+
["1", "1", "0", "0", "0"],
237+
["1", "1", "0", "0", "0"],
238+
["1", "0", "1", "0", "0"],
239+
["1", "0", "0", "1", "1"]
240+
]);
220241

221242
// numIslands([
222243
// ["1", "1", "1"],
@@ -260,25 +281,25 @@ const findNeighbours = (current_node, map, attr, x_operator, y_operator, neighbo
260281
// ["1", "0", "1", "1", "1", "1", "1", "1", "1", "0"]
261282
// ])
262283

263-
numIslands([
264-
["0", "1", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "1"],
265-
["1", "0", "1", "0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0"],
266-
["0", "1", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "1"],
267-
["1", "1", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "1", "0"],
268-
["0", "1", "0", "1", "1", "0", "1", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "1"],
269-
["1", "0", "0", "1", "0", "1", "0", "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "0"],
270-
["1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1"],
271-
["1", "0", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0", "1"],
272-
["1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1"],
273-
["0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "1", "0"],
274-
["1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "0", "1", "1", "0", "0", "0"],
275-
["0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "1", "0"],
276-
["1", "0", "0", "0", "1", "1", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1"],
277-
["0", "0", "0", "0", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0"],
278-
["0", "1", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "1", "0", "1", "0"],
279-
["1", "0", "1", "1", "1", "1", "1", "1", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "1"],
280-
["1", "0", "0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1"],
281-
["0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0"],
282-
["0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "0", "1", "1"],
283-
["1", "0", "0", "0", "1", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0"]
284-
])
284+
// numIslands([
285+
// ["0", "1", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "1"],
286+
// ["1", "0", "1", "0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0"],
287+
// ["0", "1", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "1"],
288+
// ["1", "1", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "1", "0"],
289+
// ["0", "1", "0", "1", "1", "0", "1", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "1"],
290+
// ["1", "0", "0", "1", "0", "1", "0", "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "0"],
291+
// ["1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1"],
292+
// ["1", "0", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0", "1"],
293+
// ["1", "0", "0", "1", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1"],
294+
// ["0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "1", "0"],
295+
// ["1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "0", "1", "1", "0", "0", "0"],
296+
// ["0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "1", "0"],
297+
// ["1", "0", "0", "0", "1", "1", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1"],
298+
// ["0", "0", "0", "0", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0"],
299+
// ["0", "1", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "1", "0", "1", "0"],
300+
// ["1", "0", "1", "1", "1", "1", "1", "1", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "1"],
301+
// ["1", "0", "0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1"],
302+
// ["0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0"],
303+
// ["0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "0", "1", "1"],
304+
// ["1", "0", "0", "0", "1", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0"]
305+
// ])

‎200/o_solution.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Solution from chatgpt -> Runtime - Beats 68.64%, Memory - Beats 62.72%
2+
function numIslands(grid) {
3+
if (!grid || grid.length === 0) {
4+
return 0;
5+
}
6+
7+
let count = 0;
8+
9+
const dfs = (i, j) => {
10+
if (
11+
i < 0 ||
12+
i >= grid.length ||
13+
j < 0 ||
14+
j >= grid[i].length ||
15+
grid[i][j] === "0"
16+
) {
17+
return;
18+
}
19+
20+
grid[i][j] = "0";
21+
22+
dfs(i + 1, j);
23+
dfs(i - 1, j);
24+
dfs(i, j + 1);
25+
dfs(i, j - 1);
26+
};
27+
28+
for (let i = 0; i < grid.length; i++) {
29+
for (let j = 0; j < grid[i].length; j++) {
30+
if (grid[i][j] === "1") {
31+
count++;
32+
dfs(i, j);
33+
}
34+
}
35+
}
36+
37+
return count;
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.