g0501_0600.s0547_number_of_provinces.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0547_number_of_provinces;
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
import java.util.Arrays;
public class Solution {
public int findCircleNum(int[][] arr) {
int[] parent = new int[arr.length];
Arrays.fill(parent, -1);
int ans = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr[i].length; j++) {
if (arr[i][j] == 1) {
ans += union(i, j, parent);
}
}
}
return arr.length - ans;
}
private int union(int a, int b, int[] arr) {
int ga = find(a, arr);
int gb = find(b, arr);
if (ga != gb) {
arr[gb] = ga;
return 1;
}
return 0;
}
private int find(int a, int[] arr) {
if (arr[a] == -1) {
return a;
}
arr[a] = find(arr[a], arr);
return arr[a];
}
}