-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path952. Largest Component Size by Common Factor.java
50 lines (50 loc) · 1.7 KB
/
952. Largest Component Size by Common Factor.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
class Solution {
class UnionFindAlgorithm {
public static final int find (int node, int[] parent) {
if (parent[node]==-1) {
return node;
} else {
int rparent = find(parent[node], parent);
parent[node] = rparent;
return rparent;
}
}
public static final void union(int u, int v, int[] parent) {
int unode = find(u, parent);
int vnode = find(v, parent);
if (unode != vnode) {
parent[vnode] = unode;
}
}
}
public int largestComponentSize(int[] nums) {
int[] parent = new int[100005];
Arrays.fill(parent, -1);
for (int node : nums) {
for (int i = 2; i <= (int) Math.sqrt(node); i++) {
if (node % i == 0) {
// means divisable
UnionFindAlgorithm.union(i, node, parent); // 20
UnionFindAlgorithm.union(node, (int) node/i, parent);
}
}
}
// System.out.println(parent);
int answer = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int x : nums) {
int xp = UnionFindAlgorithm.find(x, parent);
map.put(xp, map.getOrDefault(xp, 0) + 1 );
answer = Math.max(answer, map.get(xp) );
}
return answer;
}
}