Skip to content

Commit c7d4f39

Browse files
update 200 with more comments
1 parent 967469c commit c7d4f39

File tree

1 file changed

+8
-3
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+8
-3
lines changed

src/main/java/com/fishercoder/solutions/_200.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public UnionFind(char[][] grid) {
5252
for (int i = 0; i < m; i++) {
5353
for (int j = 0; j < n; j++) {
5454
if (grid[i][j] == '1') {
55+
//at initialization, we count each '1' as one island, later, during traversal, we'll union them during which we'll dedup the number of islands.
5556
count++;
56-
ids[i * n + j] = i * n + j;
5757
}
58+
ids[i * n + j] = i * n + j;
5859
}
5960
}
6061
}
@@ -63,9 +64,13 @@ public void union(int i, int j) {
6364
int x = find(i);
6465
int y = find(j);
6566
if (x != y) {
66-
/**note: this is when x != y, only in this case, we should union these two nodes, which makes sense naturally.*/
67-
count--;
67+
/**
68+
* This means when these two nodes should be unioned, however, so far,
69+
* they have not, i.e. they have different ids,
70+
* so we'll have to unify them by assigning one's ids to the other, or vice versa.
71+
* */
6872
ids[x] = y;//ids[y] = x; //also works
73+
count--;//since now these two islands are unified/merged, we'll decrement the count by one
6974
}
7075
}
7176

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy