forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
35 lines (35 loc) · 980 Bytes
/
Solution.ts
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
function maximumDetonation(bombs: number[][]): number {
const n = bombs.length;
const g: number[][] = Array.from({ length: n }, () => []);
for (let i = 0; i < n - 1; ++i) {
const [x1, y1, r1] = bombs[i];
for (let j = i + 1; j < n; ++j) {
const [x2, y2, r2] = bombs[j];
const d = Math.hypot(x1 - x2, y1 - y2);
if (d <= r1) {
g[i].push(j);
}
if (d <= r2) {
g[j].push(i);
}
}
}
let ans = 0;
for (let k = 0; k < n; ++k) {
const vis: Set<number> = new Set([k]);
const q: number[] = [k];
for (const i of q) {
for (const j of g[i]) {
if (!vis.has(j)) {
vis.add(j);
q.push(j);
}
}
}
if (vis.size === n) {
return n;
}
ans = Math.max(ans, vis.size);
}
return ans;
}