We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 58d573b commit ac2b278Copy full SHA for ac2b278
C++/largest-component-size-by-common-factor.cpp
@@ -4,19 +4,14 @@
4
class Solution {
5
public:
6
int largestComponentSize(vector<int>& A) {
7
+ UnionFind union_find(A.size());
8
unordered_map<int, vector<int>> nodesWithCommonFactor;
9
for (int i = 0; i < A.size(); ++i) {
10
for (const auto& factor : primeFactors(A[i])) {
11
nodesWithCommonFactor[factor].emplace_back(i);
12
+ union_find.union_set(nodesWithCommonFactor[factor].front(), i);
13
}
14
-
- UnionFind union_find(A.size());
15
- for (const auto& kvp : nodesWithCommonFactor) {
16
- for (const auto& node : kvp.second) {
17
- union_find.union_set(kvp.second[0], node);
18
- }
19
20
return union_find.max_size();
21
22
0 commit comments