547. Number of Provinces
Previous323. Number of Connected Components in an Undirected GraphNext990. Satisfiability of Equality Equations
Last updated
Last updated
time: O(edges*nodes) => ๆๆณๆ่ฉฒไธๆฏ
t
space: O(n)
ๅ ็บๆ็ฐ(has same root)ๅฐฑไธ่ฝๆงๆไธๅ้,
ๆไปฅ็ก็ฐ(x !=y)ๆ, ๅฏไปฅๆ็ซไธๅ้, ไนๅฐฑๆๅฐๆไธๅ circle num
็ถ isConnected[i][j] == 1 => ๅปๆพๆฏไธๆฏๅฐๆไนๆฏ1 [j][i] ไนๅฐฑๆฏๅฉ็จ union find => ๅฆๆๆฒๆcirce => ไปฃ่กจๆ้ .=> ไปฃ่กจๆฏๅไธๅgroup => res--
class Solution {
public int findCircleNum(int[][] isConnected) {
// use union find
int n = isConnected.length;
int res = n;
int roots[] = new int[n];
for (int i = 0; i < n; i++) {
roots[i] = -1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < isConnected[0].length; j++) {
if (isConnected[i][j] == 1) {
int x = find(roots, i);
int y = find(roots, j);
if (x != y) {
roots[x] = y;
res--;
}
}
}
}
return res;
}
private int find(int[] roots, int i) {
while (roots[i] != -1) {
i = roots[i];
}
return i;
}
}
class Solution {
public int findCircleNum(int[][] isConnected) {
// use union find
// so 0,0 is not
if (isConnected == null || isConnected.length == 0) return 0;
int res = isConnected.length;
int n = isConnected.length;
int[] roots = new int[n];
for (int i = 0; i < n; i++) {
roots[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (isConnected[i][j] == 1) {
res = union(roots, i, j, res);
}
}
}
return res;
}
private int union(int roots[], int i , int j, int res) {
int x = find(roots, i);
int y = find(roots, j);
if (x != y) {
roots[x] = y;
res--;
}
return res;
}
private int find(int roots[], int i) {
while (roots[i] != i) {
roots[i] = roots[roots[i]]; // add path compression
i = roots[i];
}
return i;
}
}
class Solution {
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
DSU dsu = new DSU(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (isConnected[i][j] == 1) {
dsu.union(i, j);
}
}
}
return dsu.res;
}
class DSU {
int roots[];
int res;
DSU(int n) {
roots = new int[n];
res = n;
for (int i = 0 ; i < n; i++) {
roots[i] = i;
}
}
private void union(int i, int j) {
int x = find(i);
int y = find(j);
if (x != y) {
roots[x] = y;
res--;
}
}
private int find(int x) {
if (roots[x] != x) {
roots[x] = find(roots[x]);
}
return roots[x];
}
}
}
class Solution {
public int findCircleNum(int[][] isConnected) {
int[] visited = new int[isConnected.length]; // dont need init to 0
int count = 0;
for (int i = 0; i < isConnected.length; i++) {
if (visited[i] == 0) {
dfs(isConnected, visited, i);
count++;
}
}
return count;
}
public void dfs(int[][] isConnected, int[] visited, int i) {
for (int j = 0; j < isConnected.length; j++) {
if (isConnected[i][j] == 1 && visited[j] == 0) {
visited[j] = 1;
dfs(isConnected, visited, j);
}
}
}
}