-
Notifications
You must be signed in to change notification settings - Fork 277
/
LargestComponentSizeByCommonFactor.java
59 lines (51 loc) · 1.22 KB
/
LargestComponentSizeByCommonFactor.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
class Solution {
private class UnionFind {
private int[] parent;
private UnionFind(int n){
parent = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
}
}
private int getAbsoluteParent(int i){
if(parent[i]==i){
// absolute parent
return i;
}
parent[i]=getAbsoluteParent(parent[i]);
return parent[i];
}
private void unify(int i, int j){
int absoluteParentI = getAbsoluteParent(i);
int absoluteParentJ = getAbsoluteParent(j);
if(absoluteParentI!=absoluteParentJ){
// 7->2, 21->2 (7,21)
parent[absoluteParentJ] = absoluteParentI;
}
}
}
public int largestComponentSize(int[] nums) {
int max=0;
for(int el:nums) {
max = Math.max(el, max);
}
UnionFind uf = new UnionFind(max+1);
for(int i:nums) {
for(int j=2;j*j<=i;j++) {
if(i%j==0){ // j is a factor of i
uf.unify( j,i); // i=21, j=3
uf.unify(i/j,i); // (21,3), (21,21/3(7))
}
}
}
//<Parent, Freq>
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int maxComponet = 0;
for(int el:nums){
int parent = uf.getAbsoluteParent(el);
map.put(parent, map.getOrDefault(parent, 0) + 1);
maxComponet= Math.max(maxComponet, map.get(parent));
}
return maxComponet;
}
}