Skip to content

Commit 3b8abbc

Browse files
solves #952: Largest Component Size by Common Factor in java
1 parent 98b4999 commit 3b8abbc

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@
458458
| 942 | [DI String Match](https://leetcode.com/problems/di-string-match) | [![Java](assets/java.png)](src/DIStringMatch.java) | |
459459
| 944 | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted) | [![Java](assets/java.png)](src/DeleteColumnsToMakeSorted.java) | |
460460
| 949 | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits) | | |
461+
| 952 | [Largest Component Size by Common Factor](https://leetcode.com/problems/largest-component-size-by-common-factor) | [![Java](assets/java.png)](src/LargestComponentSizeByCommonFactor.java) | |
461462
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary) | [![Java](assets/java.png)](src/VerifyAnAlienDictionary.java) | |
462463
| 961 | [N-Repeated Elements in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array) | [![Java](assets/java.png)](src/NRepeatedElementInSizeNArray.java) | |
463464
| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree) | [![Java](assets/java.png)](src/UnivaluedBinaryTree.java) | |

Diff for: src/LargestComponentSizeByCommonFactor.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// https://leetcode.com/problems/largest-component-size-by-common-factor
2+
// N = nums, M = max(N)
3+
// T: O(M + N sqrt(M) al(M) + N al(N)) al = inverse Ackermann function
4+
// S: O(M + N)
5+
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class LargestComponentSizeByCommonFactor {
11+
private static final class DisjointSet {
12+
private final int[] root, rank;
13+
14+
public DisjointSet(int size) {
15+
root = new int[size];
16+
rank = new int[size];
17+
for (int i = 0 ; i < size ; i++) {
18+
root[i] = i;
19+
rank[i] = 1;
20+
}
21+
}
22+
23+
public int find(int num) {
24+
if (num == root[num]) {
25+
return num;
26+
}
27+
return root[num] = find(root[num]);
28+
}
29+
30+
public boolean areConnected(int x, int y) {
31+
return find(x) == find(y);
32+
}
33+
34+
public void union(int x, int y) {
35+
final int rootX = find(x), rootY = find(y);
36+
if (rootX == rootY) {
37+
return;
38+
}
39+
if (rank[rootX] > rank[rootY]) {
40+
root[rootY] = rootX;
41+
} else if (rank[rootX] < rank[rootY]) {
42+
root[rootY] = rootX;
43+
} else {
44+
root[rootY] = rootX;
45+
rank[rootX]++;
46+
}
47+
}
48+
49+
public int size() {
50+
return root.length;
51+
}
52+
}
53+
54+
public int largestComponentSize(int[] nums) {
55+
final int maxValue = Arrays.stream(nums).max().getAsInt();
56+
final DisjointSet disjointSet = new DisjointSet(maxValue + 1);
57+
for (int number : nums) {
58+
for (int i = 2 ; i * i <= number ; i++) {
59+
if (number % i == 0) {
60+
disjointSet.union(number, i);
61+
disjointSet.union(number, number/ i);
62+
}
63+
}
64+
}
65+
return largestComponentSize(disjointSet, nums);
66+
}
67+
68+
private static int largestComponentSize(DisjointSet disjointSet, int[] numbers) {
69+
final Map<Integer, Integer> rootFrequencies = new HashMap<>();
70+
int maxSize = 1;
71+
for (int number : numbers) {
72+
final int root = disjointSet.find(number);
73+
rootFrequencies.put(root, rootFrequencies.getOrDefault(root, 0) + 1);
74+
maxSize = Math.max(maxSize, rootFrequencies.get(root));
75+
}
76+
return maxSize;
77+
}
78+
}

0 commit comments

Comments
 (0)