File tree Expand file tree Collapse file tree 3 files changed +148
-0
lines changed
solution/0900-0999/0959.Regions Cut By Slashes Expand file tree Collapse file tree 3 files changed +148
-0
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,57 @@ func regionsBySlashes(grid []string) int {
279
279
}
280
280
```
281
281
282
+ #### TypeScript
283
+
284
+ ``` ts
285
+ function regionsBySlashes(grid : string []): number {
286
+ const find = (x : number ) => {
287
+ if (p [x ] !== x ) {
288
+ p [x ] = find (p [x ]);
289
+ }
290
+ return p [x ];
291
+ };
292
+
293
+ const union = (a : number , b : number ) => {
294
+ const pa = find (a );
295
+ const pb = find (b );
296
+ if (pa !== pb ) {
297
+ p [pa ] = pb ;
298
+ size -- ;
299
+ }
300
+ };
301
+
302
+ const n = grid .length ;
303
+ let size = n * n * 4 ;
304
+ const p = Array .from ({ length: size }, (_ , i ) => i );
305
+
306
+ for (let i = 0 ; i < n ; i ++ ) {
307
+ for (let j = 0 ; j < n ; j ++ ) {
308
+ const k = i * n + j ;
309
+ if (i < n - 1 ) {
310
+ union (4 * k + 2 , (k + n ) * 4 );
311
+ }
312
+ if (j < n - 1 ) {
313
+ union (4 * k + 1 , (k + 1 ) * 4 + 3 );
314
+ }
315
+ if (grid [i ][j ] === ' /' ) {
316
+ union (4 * k , 4 * k + 3 );
317
+ union (4 * k + 1 , 4 * k + 2 );
318
+ } else if (grid [i ][j ] === ' \\ ' ) {
319
+ union (4 * k , 4 * k + 1 );
320
+ union (4 * k + 2 , 4 * k + 3 );
321
+ } else {
322
+ union (4 * k , 4 * k + 1 );
323
+ union (4 * k + 1 , 4 * k + 2 );
324
+ union (4 * k + 2 , 4 * k + 3 );
325
+ }
326
+ }
327
+ }
328
+
329
+ return size ;
330
+ }
331
+ ```
332
+
282
333
#### JavaScript
283
334
284
335
``` js
Original file line number Diff line number Diff line change @@ -269,6 +269,57 @@ func regionsBySlashes(grid []string) int {
269
269
}
270
270
```
271
271
272
+ #### TypeScript
273
+
274
+ ``` ts
275
+ function regionsBySlashes(grid : string []): number {
276
+ const find = (x : number ) => {
277
+ if (p [x ] !== x ) {
278
+ p [x ] = find (p [x ]);
279
+ }
280
+ return p [x ];
281
+ };
282
+
283
+ const union = (a : number , b : number ) => {
284
+ const pa = find (a );
285
+ const pb = find (b );
286
+ if (pa !== pb ) {
287
+ p [pa ] = pb ;
288
+ size -- ;
289
+ }
290
+ };
291
+
292
+ const n = grid .length ;
293
+ let size = n * n * 4 ;
294
+ const p = Array .from ({ length: size }, (_ , i ) => i );
295
+
296
+ for (let i = 0 ; i < n ; i ++ ) {
297
+ for (let j = 0 ; j < n ; j ++ ) {
298
+ const k = i * n + j ;
299
+ if (i < n - 1 ) {
300
+ union (4 * k + 2 , (k + n ) * 4 );
301
+ }
302
+ if (j < n - 1 ) {
303
+ union (4 * k + 1 , (k + 1 ) * 4 + 3 );
304
+ }
305
+ if (grid [i ][j ] === ' /' ) {
306
+ union (4 * k , 4 * k + 3 );
307
+ union (4 * k + 1 , 4 * k + 2 );
308
+ } else if (grid [i ][j ] === ' \\ ' ) {
309
+ union (4 * k , 4 * k + 1 );
310
+ union (4 * k + 2 , 4 * k + 3 );
311
+ } else {
312
+ union (4 * k , 4 * k + 1 );
313
+ union (4 * k + 1 , 4 * k + 2 );
314
+ union (4 * k + 2 , 4 * k + 3 );
315
+ }
316
+ }
317
+ }
318
+
319
+ return size ;
320
+ }
321
+ ```
322
+
272
323
#### JavaScript
273
324
274
325
``` js
Original file line number Diff line number Diff line change
1
+ function regionsBySlashes ( grid : string [ ] ) : number {
2
+ const find = ( x : number ) => {
3
+ if ( p [ x ] !== x ) {
4
+ p [ x ] = find ( p [ x ] ) ;
5
+ }
6
+ return p [ x ] ;
7
+ } ;
8
+
9
+ const union = ( a : number , b : number ) => {
10
+ const pa = find ( a ) ;
11
+ const pb = find ( b ) ;
12
+ if ( pa !== pb ) {
13
+ p [ pa ] = pb ;
14
+ size -- ;
15
+ }
16
+ } ;
17
+
18
+ const n = grid . length ;
19
+ let size = n * n * 4 ;
20
+ const p = Array . from ( { length : size } , ( _ , i ) => i ) ;
21
+
22
+ for ( let i = 0 ; i < n ; i ++ ) {
23
+ for ( let j = 0 ; j < n ; j ++ ) {
24
+ const k = i * n + j ;
25
+ if ( i < n - 1 ) {
26
+ union ( 4 * k + 2 , ( k + n ) * 4 ) ;
27
+ }
28
+ if ( j < n - 1 ) {
29
+ union ( 4 * k + 1 , ( k + 1 ) * 4 + 3 ) ;
30
+ }
31
+ if ( grid [ i ] [ j ] === '/' ) {
32
+ union ( 4 * k , 4 * k + 3 ) ;
33
+ union ( 4 * k + 1 , 4 * k + 2 ) ;
34
+ } else if ( grid [ i ] [ j ] === '\\' ) {
35
+ union ( 4 * k , 4 * k + 1 ) ;
36
+ union ( 4 * k + 2 , 4 * k + 3 ) ;
37
+ } else {
38
+ union ( 4 * k , 4 * k + 1 ) ;
39
+ union ( 4 * k + 1 , 4 * k + 2 ) ;
40
+ union ( 4 * k + 2 , 4 * k + 3 ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+ return size ;
46
+ }
You can’t perform that action at this time.
0 commit comments