1
1
package org .sean .array ;
2
2
3
+ import org .sean .graph .UnionFind ;
4
+
3
5
/***
4
6
* 200. Number of Islands
5
7
*/
6
8
public class IslandCounter {
7
- private int [] parent ; // parent[i] = parent of i
8
- private int [] size ; // size[i] = number of sites in subtree rooted at i
9
- private int count ; // number of components
10
-
11
- private void init (int n ) {
12
- count = n ;
13
- parent = new int [n ];
14
- size = new int [n ];
15
- for (int i = 0 ; i < n ; i ++) {
16
- parent [i ] = i ;
17
- size [i ] = 1 ;
18
- }
19
- }
20
-
21
- /**
22
- * The number of components.
23
- */
24
- private int count () {
25
- return count ;
26
- }
27
-
28
- private int find (int p ) {
29
- validate (p );
30
- while (p != parent [p ])
31
- p = parent [p ];
32
- return p ;
33
- }
34
-
35
- // validate that p is a valid index
36
- private void validate (int p ) {
37
- int n = parent .length ;
38
- if (p < 0 || p >= n ) {
39
- throw new IllegalArgumentException ("index " + p + " is not between 0 and " + (n - 1 ));
40
- }
41
- }
42
-
43
- private boolean connected (int p , int q ) {
44
- return find (p ) == find (q );
45
- }
46
-
47
- private void union (int p , int q ) {
48
- int rootP = find (p );
49
- int rootQ = find (q );
50
- if (rootP == rootQ ) return ;
51
-
52
- // make smaller root point to larger one
53
- if (size [rootP ] < size [rootQ ]) {
54
- parent [rootP ] = rootQ ;
55
- size [rootQ ] += size [rootP ];
56
- } else {
57
- parent [rootQ ] = rootP ;
58
- size [rootP ] += size [rootQ ];
59
- }
60
- count --;
61
- }
62
-
63
- private void link (int p , int q ) {
64
- if (!connected (p , q )) {
65
- union (p , q );
66
- }
67
- }
9
+ private UnionFind unionFind ;
68
10
69
11
public int numIslands (char [][] grid ) {
70
- if (grid == null || grid .length == 0 )
71
- return 0 ;
12
+ if (grid == null || grid .length == 0 ) return 0 ;
72
13
int row = grid .length ;
73
14
int col = grid [0 ].length ;
74
15
75
16
int n = row * col ;
76
17
int p = -1 ;
77
18
int q = -1 ;
78
19
79
- // take advantage of WeightedQuickUnionUF from Algs4
80
- // https://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.html
81
- init (n );
20
+ unionFind = new UnionFind (n );
82
21
83
22
int zeroCnt = 0 ;
84
23
@@ -90,15 +29,15 @@ public int numIslands(char[][] grid) {
90
29
if (i + 1 < row ) {
91
30
if (grid [i + 1 ][j ] == '1' ) {
92
31
int p2 = (i + 1 ) * col + j ;
93
- link (pos , p2 );
32
+ unionFind . union (pos , p2 );
94
33
95
34
System .out .println (String .format (">>> Evaluate pos [%d, %d]" , pos , p2 ));
96
35
}
97
36
}
98
37
if (j + 1 < col ) {
99
38
if (grid [i ][j + 1 ] == '1' ) {
100
39
int p2 = i * col + j + 1 ;
101
- link (pos , p2 );
40
+ unionFind . union (pos , p2 );
102
41
103
42
System .out .println (String .format (">>> Evaluate pos [%d, %d]" , pos , p2 ));
104
43
}
@@ -109,6 +48,6 @@ public int numIslands(char[][] grid) {
109
48
}
110
49
}
111
50
112
- return count () - zeroCnt ;
51
+ return unionFind . count () - zeroCnt ;
113
52
}
114
53
}
0 commit comments