Detonate the Maximum Bombs LeetCode solution in JavaScript

1
/**
2
* @param {number[][]} bombs
3
* @return {number}
4
*/
5
var maximumDetonation = function (bombs) {
6
if (bombs.length <= 1) return bombs.length;
7

8
let adj = {},
9
maxSize = 0;
10
const checkIfInsideRange = (x, y, center_x, center_y, radius) => {
11
return (x - center_x) ** 2 + (y - center_y) ** 2 <= radius ** 2;
12
};
13

14
//CREATE ADJACENCY MATRIX
15
for (let i = 0; i < bombs.length; i++) {
16
for (let j = i + 1; j < bombs.length; j++) {
17
if (!adj[i]) adj[i] = [];
18
if (!adj[j]) adj[j] = [];
19
let bombOne = bombs[i];
20
let bombTwo = bombs[j];
21
let fir = checkIfInsideRange(
22
bombOne[0],
23
bombOne[1],
24
bombTwo[0],
25
bombTwo[1],
26
bombOne[2]
27
);
28
if (fir) adj[i].push(j);
29
let sec = checkIfInsideRange(
30
bombOne[0],
31
bombOne[1],
32
bombTwo[0],
33
bombTwo[1],
34
bombTwo[2]
35
);
36
if (sec) adj[j].push(i);
37
}
38
}
39

40
//DEPTH FIRST SEARCH TO FIND ALL BOMBS TRIGGERED BY NODE
41
const dfs = (node, visited) => {
42
let detonated = 1;
43
visited[node] = true;
44
let childs = adj[node] || [];
45
for (let child of childs) {
46
if (visited[child]) continue;
47
detonated += dfs(child, visited);
48
}
49
maxSize = Math.max(maxSize, detonated);
50
return detonated;
51
};
52

53
for (let i = 0; i < bombs.length; i++) {
54
dfs(i, {});
55
}
56
return maxSize;
57
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0