Skip to content

Instantly share code, notes, and snippets.

@adrianosela
Created September 3, 2019 19:37
Show Gist options
  • Save adrianosela/fc1f28afaa483e37af3ffe3b265e82c3 to your computer and use it in GitHub Desktop.
Save adrianosela/fc1f28afaa483e37af3ffe3b265e82c3 to your computer and use it in GitHub Desktop.
class Solution {
public int findCircleNum(int[][] M) {
if (M == null || M.length == 0) {
return 0;
}
int nodes = M.length;
// initialize disjoint sets where every node
// is its own parent
int parents[] = new int[nodes];
for (int node = 0; node < nodes; node++) {
parents[node] = node;
}
for (int node_a = 0; node_a < nodes; node_a++) {
for (int node_b = node_a + 1; node_b < nodes; node_b++) {
if (M[node_a][node_b] == 1) {
union(parents, node_a, node_b);
}
}
}
Set<Integer> s = new HashSet();
for (int node = 0; node < nodes; node++) {
s.add(find(parents, node));
}
return s.size();
}
void union(int[] p, int node_a, int node_b) {
p[find(p, node_b)] = find(p, node_a);
}
int find(int[] p, int node) {
while (p[node] != node) {
node = p[node];
}
return p[node];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment