@@ -60,7 +60,7 @@ Union find.
60
60
``` python
61
61
class Solution :
62
62
def closedIsland (self , grid : List[List[int ]]) -> int :
63
- def dfs (i , j ) :
63
+ def dfs (i : int , j : int ) -> int :
64
64
res = int (0 < i < m - 1 and 0 < j < n - 1 )
65
65
grid[i][j] = 1
66
66
for a, b in pairwise(dirs):
@@ -76,16 +76,16 @@ class Solution:
76
76
77
77
``` python
78
78
class UnionFind :
79
- def __init__ (self , n ):
79
+ def __init__ (self , n : int ):
80
80
self .p = list (range (n))
81
81
self .size = [1 ] * n
82
82
83
- def find (self , x ) :
83
+ def find (self , x : int ) -> int :
84
84
if self .p[x] != x:
85
85
self .p[x] = self .find(self .p[x])
86
86
return self .p[x]
87
87
88
- def union (self , a , b ):
88
+ def union (self , a : int , b : int ):
89
89
pa, pb = self .find(a), self .find(b)
90
90
if pa != pb:
91
91
if self .size[pa] > self .size[pb]:
@@ -411,6 +411,206 @@ func closedIsland(grid [][]int) (ans int) {
411
411
}
412
412
```
413
413
414
+ ### ** TypeScript**
415
+
416
+ ``` ts
417
+ function closedIsland(grid : number [][]): number {
418
+ const m = grid .length ;
419
+ const n = grid [0 ].length ;
420
+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
421
+ const dfs = (i : number , j : number ): number => {
422
+ let res = i > 0 && j > 0 && i < m - 1 && j < n - 1 ? 1 : 0 ;
423
+ grid [i ][j ] = 1 ;
424
+ for (let k = 0 ; k < 4 ; ++ k ) {
425
+ const [x, y] = [i + dirs [k ], j + dirs [k + 1 ]];
426
+ if (x >= 0 && y >= 0 && x < m && y < n && grid [x ][y ] === 0 ) {
427
+ res &= dfs (x , y );
428
+ }
429
+ }
430
+ return res ;
431
+ };
432
+ let ans = 0 ;
433
+ for (let i = 0 ; i < m ; ++ i ) {
434
+ for (let j = 0 ; j < n ; j ++ ) {
435
+ if (grid [i ][j ] === 0 ) {
436
+ ans += dfs (i , j );
437
+ }
438
+ }
439
+ }
440
+ return ans ;
441
+ }
442
+ ```
443
+
444
+ ``` ts
445
+ function closedIsland(grid : number [][]): number {
446
+ const m = grid .length ;
447
+ const n = grid [0 ].length ;
448
+ const uf = new UnionFind (m * n + 1 );
449
+ for (let i = 0 ; i < m ; ++ i ) {
450
+ for (let j = 0 ; j < n ; ++ j ) {
451
+ if (i === 0 || i === m - 1 || j === 0 || j === n - 1 ) {
452
+ uf .union (i * n + j , m * n );
453
+ }
454
+ if (grid [i ][j ] === 0 ) {
455
+ if (i + 1 < m && grid [i + 1 ][j ] === 0 ) {
456
+ uf .union (i * n + j , (i + 1 ) * n + j );
457
+ }
458
+ if (j + 1 < n && grid [i ][j + 1 ] === 0 ) {
459
+ uf .union (i * n + j , i * n + j + 1 );
460
+ }
461
+ }
462
+ }
463
+ }
464
+ let ans = 0 ;
465
+ for (let i = 0 ; i < m ; ++ i ) {
466
+ for (let j = 0 ; j < n ; j ++ ) {
467
+ if (grid [i ][j ] === 0 && uf .find (i * n + j ) === i * n + j ) {
468
+ ++ ans ;
469
+ }
470
+ }
471
+ }
472
+ return ans ;
473
+ }
474
+
475
+ class UnionFind {
476
+ private p: number [];
477
+ private size: number [];
478
+
479
+ constructor (n : number ) {
480
+ this .p = Array (n )
481
+ .fill (0 )
482
+ .map ((_ , i ) => i );
483
+ this .size = Array (n ).fill (1 );
484
+ }
485
+
486
+ find(x : number ): number {
487
+ if (this .p [x ] !== x ) {
488
+ this .p [x ] = this .find (this .p [x ]);
489
+ }
490
+ return this .p [x ];
491
+ }
492
+
493
+ union(a : number , b : number ): void {
494
+ const [pa, pb] = [this .find (a ), this .find (b )];
495
+ if (pa === pb ) {
496
+ return ;
497
+ }
498
+ if (this .size [pa ] > this .size [pb ]) {
499
+ this .p [pb ] = pa ;
500
+ this .size [pa ] += this .size [pb ];
501
+ } else {
502
+ this .p [pa ] = pb ;
503
+ this .size [pb ] += this .size [pa ];
504
+ }
505
+ }
506
+ }
507
+ ```
508
+
509
+ ### ** C#**
510
+
511
+ ``` cs
512
+ public class Solution {
513
+ private int m ;
514
+ private int n ;
515
+ private int [][] grid ;
516
+
517
+ public int ClosedIsland (int [][] grid ) {
518
+ m = grid .Length ;
519
+ n = grid [0 ].Length ;
520
+ this .grid = grid ;
521
+ int ans = 0 ;
522
+ for (int i = 0 ; i < m ; ++ i ) {
523
+ for (int j = 0 ; j < n ; ++ j ) {
524
+ if (grid [i ][j ] == 0 ) {
525
+ ans += dfs (i , j );
526
+ }
527
+ }
528
+ }
529
+ return ans ;
530
+ }
531
+
532
+ private int dfs (int i , int j ) {
533
+ int res = i > 0 && i < m - 1 && j > 0 && j < n - 1 ? 1 : 0 ;
534
+ grid [i ][j ] = 1 ;
535
+ int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
536
+ for (int k = 0 ; k < 4 ; ++ k ) {
537
+ int x = i + dirs [k ], y = j + dirs [k + 1 ];
538
+ if (x >= 0 && x < m && y >= 0 && y < n && grid [x ][y ] == 0 ) {
539
+ res &= dfs (x , y );
540
+ }
541
+ }
542
+ return res ;
543
+ }
544
+ }
545
+ ```
546
+
547
+ ``` cs
548
+ class UnionFind {
549
+ private int [] p ;
550
+ private int [] size ;
551
+
552
+ public UnionFind (int n ) {
553
+ p = new int [n ];
554
+ size = new int [n ];
555
+ for (int i = 0 ; i < n ; ++ i ) {
556
+ p [i ] = i ;
557
+ size [i ] = 1 ;
558
+ }
559
+ }
560
+
561
+ public int find (int x ) {
562
+ if (p [x ] != x ) {
563
+ p [x ] = find (p [x ]);
564
+ }
565
+ return p [x ];
566
+ }
567
+
568
+ public void union (int a , int b ) {
569
+ int pa = find (a ), pb = find (b );
570
+ if (pa != pb ) {
571
+ if (size [pa ] > size [pb ]) {
572
+ p [pb ] = pa ;
573
+ size [pa ] += size [pb ];
574
+ } else {
575
+ p [pa ] = pb ;
576
+ size [pb ] += size [pa ];
577
+ }
578
+ }
579
+ }
580
+ }
581
+
582
+ public class Solution {
583
+ public int ClosedIsland (int [][] grid ) {
584
+ int m = grid .Length , n = grid [0 ].Length ;
585
+ UnionFind uf = new UnionFind (m * n + 1 );
586
+ for (int i = 0 ; i < m ; ++ i ) {
587
+ for (int j = 0 ; j < n ; ++ j ) {
588
+ if (i == 0 || i == m - 1 || j == 0 || j == n - 1 ) {
589
+ uf .union (i * n + j , m * n );
590
+ }
591
+ if (grid [i ][j ] == 0 ) {
592
+ if (i + 1 < m && grid [i + 1 ][j ] == 0 ) {
593
+ uf .union (i * n + j , (i + 1 ) * n + j );
594
+ }
595
+ if (j + 1 < n && grid [i ][j + 1 ] == 0 ) {
596
+ uf .union (i * n + j , i * n + j + 1 );
597
+ }
598
+ }
599
+ }
600
+ }
601
+ int ans = 0 ;
602
+ for (int i = 0 ; i < m ; ++ i ) {
603
+ for (int j = 0 ; j < n ; ++ j ) {
604
+ if (grid [i ][j ] == 0 && uf .find (i * n + j ) == i * n + j ) {
605
+ ++ ans ;
606
+ }
607
+ }
608
+ }
609
+ return ans ;
610
+ }
611
+ }
612
+ ```
613
+
414
614
### ** ...**
415
615
416
616
```
0 commit comments