Skip to content

Commit 8ebdbca

Browse files
committed
feat: add ts solution to lc problem: No.2101
1 parent 1da0998 commit 8ebdbca

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

solution/2100-2199/2101.Detonate the Maximum Bombs/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,45 @@ func maximumDetonation(bombs [][]int) int {
263263
}
264264
```
265265

266+
#### TypeScript
267+
268+
```ts
269+
function maximumDetonation(bombs: number[][]): number {
270+
const n = bombs.length;
271+
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
272+
273+
for (let i = 0; i < n - 1; i++) {
274+
for (let j = 1; j < n; j++) {
275+
const [x1, y1, r1] = bombs[i];
276+
const [x2, y2, r2] = bombs[j];
277+
const distance = Math.hypot(x1 - x2, y1 - y2);
278+
279+
if (distance <= r1) g.get(i)!.push(j);
280+
if (distance <= r2) g.get(j)!.push(i);
281+
}
282+
}
283+
284+
let res = 0;
285+
for (let i = 0; i < n; i++) {
286+
const seen = new Set<number>([i]);
287+
const q = [i];
288+
289+
for (const i of q) {
290+
for (const j of g.get(i) ?? []) {
291+
if (seen.has(j)) continue;
292+
seen.add(j);
293+
q.push(j);
294+
}
295+
}
296+
297+
if (seen.size === n) return n;
298+
res = Math.max(res, seen.size);
299+
}
300+
301+
return res;
302+
}
303+
```
304+
266305
<!-- tabs:end -->
267306

268307
<!-- solution:end -->

solution/2100-2199/2101.Detonate the Maximum Bombs/README_EN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,45 @@ func maximumDetonation(bombs [][]int) int {
256256
}
257257
```
258258

259+
#### TypeScript
260+
261+
```ts
262+
function maximumDetonation(bombs: number[][]): number {
263+
const n = bombs.length;
264+
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
265+
266+
for (let i = 0; i < n - 1; i++) {
267+
for (let j = 1; j < n; j++) {
268+
const [x1, y1, r1] = bombs[i];
269+
const [x2, y2, r2] = bombs[j];
270+
const distance = Math.hypot(x1 - x2, y1 - y2);
271+
272+
if (distance <= r1) g.get(i)!.push(j);
273+
if (distance <= r2) g.get(j)!.push(i);
274+
}
275+
}
276+
277+
let res = 0;
278+
for (let i = 0; i < n; i++) {
279+
const seen = new Set<number>([i]);
280+
const q = [i];
281+
282+
for (const i of q) {
283+
for (const j of g.get(i) ?? []) {
284+
if (seen.has(j)) continue;
285+
seen.add(j);
286+
q.push(j);
287+
}
288+
}
289+
290+
if (seen.size === n) return n;
291+
res = Math.max(res, seen.size);
292+
}
293+
294+
return res;
295+
}
296+
```
297+
259298
<!-- tabs:end -->
260299

261300
<!-- solution:end -->
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function maximumDetonation(bombs: number[][]): number {
2+
const n = bombs.length;
3+
const g = new Map<number, number[]>(bombs.map((_, i) => [i, []]));
4+
5+
for (let i = 0; i < n - 1; i++) {
6+
for (let j = 1; j < n; j++) {
7+
const [x1, y1, r1] = bombs[i];
8+
const [x2, y2, r2] = bombs[j];
9+
const distance = Math.hypot(x1 - x2, y1 - y2);
10+
11+
if (distance <= r1) g.get(i)!.push(j);
12+
if (distance <= r2) g.get(j)!.push(i);
13+
}
14+
}
15+
16+
let res = 0;
17+
for (let i = 0; i < n; i++) {
18+
const seen = new Set<number>([i]);
19+
const q = [i];
20+
21+
for (const i of q) {
22+
for (const j of g.get(i) ?? []) {
23+
if (seen.has(j)) continue;
24+
seen.add(j);
25+
q.push(j);
26+
}
27+
}
28+
29+
if (seen.size === n) return n;
30+
res = Math.max(res, seen.size);
31+
}
32+
33+
return res;
34+
}

0 commit comments

Comments
 (0)