Skip to content

Commit bca43de

Browse files
authored
Create 2101-detonate-the-maximum-bombs.js
1 parent 0c193c3 commit bca43de

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

2101-detonate-the-maximum-bombs.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[][]} bombs
3+
* @return {number}
4+
*/
5+
const maximumDetonation = function(bombs) {
6+
let n = bombs.length, res = 1, graph = {}
7+
for(let i = 0; i < n; i++) {
8+
for(let j = 0; j < n; j++) {
9+
if (i === j) continue
10+
if (bombAdj(bombs[i], bombs[j])) {
11+
if (graph[i] == null) graph[i] = []
12+
graph[i].push(j)
13+
}
14+
}
15+
}
16+
function dfs(node, visited) {
17+
for(const next of (graph[node] || [])) {
18+
if(!visited.has(next)) {
19+
visited.add(next)
20+
dfs(next, visited)
21+
}
22+
}
23+
}
24+
for (let i = 0; i < n; i++) {
25+
const set = new Set([i])
26+
dfs(i, set)
27+
res = Math.max(res, set.size)
28+
}
29+
30+
return res
31+
};
32+
33+
function bombAdj(source, target) {
34+
const [x1, y1, r1] = source
35+
const [x2, y2] = target
36+
const { abs } = Math
37+
return abs(x1 - x2) ** 2 + abs(y1 - y2) ** 2 <= r1 ** 2
38+
}

0 commit comments

Comments
 (0)