-
Notifications
You must be signed in to change notification settings - Fork 0
/
L547.java
68 lines (63 loc) · 1.89 KB
/
L547.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution547 {
class Solution {
int[] rep; // Representative
int[] size; // Size of set with current member
/**
* 547. Friend Circles https://leetcode.com/problems/friend-circles/description/
*
* @param M
* @return
* @timeComplexity O(n * log n) in union find operations
* @spaceComplexity O(n)
*/
public int findCircleNum(int[][] M) {
if (M.length <= 1) {
return M.length;
}
// Init union find
rep = new int[M.length];
size = new int[M.length];
for (int i = 0; i < M.length; i++) {
rep[i] = i;
size[i] = 1;
}
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M.length; j++) {
if (M[i][j] == 1) {
union(i, j);
}
}
}
// Count unique reps
int[] uniq = new int[M.length];
int result = 0;
for (int i = 0; i < M.length; i++) {
int repI = find(i);
if (uniq[repI] == 0) {
result++;
uniq[repI] = 1;
}
}
return result;
}
// Finds the representative of x
private int find(int x) {
while (x != rep[x]) {
x = rep[x];
}
return x;
}
// Unites sets that contain x and y
private void union(int x, int y) {
int repX = find(x);
int repY = find(y);
if (size[repX] > size[repY]) {
size[repX] += size[repY];
rep[repY] = repX;
} else {
size[repY] += size[repX];
rep[repX] = repY;
}
}
}
}