Skip to content

Commit b7f9c41

Browse files
authored
Update count-connected-components-in-lcm-graph.cpp
1 parent de32821 commit b7f9c41

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

C++/count-connected-components-in-lcm-graph.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Solution {
4242
int countComponents(vector<int>& nums, int threshold) {
4343
UnionFind uf(threshold);
4444
vector<int> lookup(threshold, -1);
45+
int result = size(nums);
4546
for (const auto& x : nums) {
4647
if (x - 1 >= threshold) {
4748
continue;
@@ -51,18 +52,14 @@ class Solution {
5152
lookup[i - 1] = x - 1;
5253
continue;
5354
}
54-
uf.union_set(lookup[i - 1], x - 1);
55+
if (uf.union_set(lookup[i - 1], x - 1)) {
56+
--result;
57+
}
5558
if (i == x) {
5659
break;
5760
}
5861
}
5962
}
60-
int result = 0;
61-
for (const auto& x : nums) {
62-
if (x - 1 >= threshold || uf.find_set(x - 1) == x - 1) {
63-
++result;
64-
}
65-
}
6663
return result;
6764
}
6865
};

0 commit comments

Comments
 (0)