File tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed
solution/2100-2199/2101.Detonate the Maximum Bombs Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -263,6 +263,45 @@ func maximumDetonation(bombs [][]int) int {
263
263
}
264
264
```
265
265
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
+
266
305
<!-- tabs:end -->
267
306
268
307
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -256,6 +256,45 @@ func maximumDetonation(bombs [][]int) int {
256
256
}
257
257
```
258
258
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
+
259
298
<!-- tabs:end -->
260
299
261
300
<!-- solution:end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments