Skip to content

Commit ac2b278

Browse files
authored
Update largest-component-size-by-common-factor.cpp
1 parent 58d573b commit ac2b278

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

C++/largest-component-size-by-common-factor.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44
class Solution {
55
public:
66
int largestComponentSize(vector<int>& A) {
7+
UnionFind union_find(A.size());
78
unordered_map<int, vector<int>> nodesWithCommonFactor;
89
for (int i = 0; i < A.size(); ++i) {
910
for (const auto& factor : primeFactors(A[i])) {
1011
nodesWithCommonFactor[factor].emplace_back(i);
12+
union_find.union_set(nodesWithCommonFactor[factor].front(), i);
1113
}
1214
}
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-
}
2015
return union_find.max_size();
2116
}
2217

0 commit comments

Comments
 (0)