Skip to content

Commit 71618eb

Browse files
authored
Update 2101-detonate-the-maximum-bombs.js
1 parent 03733ab commit 71618eb

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

2101-detonate-the-maximum-bombs.js

+44
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,47 @@ function bombAdj(source, target) {
3636
const { abs } = Math
3737
return abs(x1 - x2) ** 2 + abs(y1 - y2) ** 2 <= r1 ** 2
3838
}
39+
40+
// another
41+
42+
/**
43+
* @param {number[][]} bombs
44+
* @return {number}
45+
*/
46+
const maximumDetonation = function(bombs) {
47+
const n = bombs.length, graph = {}
48+
for(let i = 0; i < n; i++) {
49+
for(let j = 0; j < n; j++) {
50+
if(i === j) continue
51+
if(adjValid(bombs[i], bombs[j])) {
52+
if(graph[i] == null) graph[i] = []
53+
graph[i].push(j)
54+
}
55+
}
56+
}
57+
58+
let res = 0
59+
for(let i = 0; i < n; i++) {
60+
const set = new Set([i])
61+
dfs(i, set)
62+
res = Math.max(res, set.size)
63+
}
64+
return res
65+
66+
function dfs(node, visited){
67+
for (const e of (graph[node] || [])) {
68+
if(!visited.has(e)) {
69+
visited.add(e)
70+
dfs(e, visited)
71+
}
72+
}
73+
}
74+
75+
function adjValid(start, target) {
76+
const [sx, sy, r] = start
77+
const [ex, ey] = target
78+
return Math.abs(sx - ex) ** 2 + Math.abs(sy - ey) ** 2 <= r ** 2
79+
}
80+
};
81+
82+

0 commit comments

Comments
 (0)