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 de32821 commit b7f9c41Copy full SHA for b7f9c41
C++/count-connected-components-in-lcm-graph.cpp
@@ -42,6 +42,7 @@ class Solution {
42
int countComponents(vector<int>& nums, int threshold) {
43
UnionFind uf(threshold);
44
vector<int> lookup(threshold, -1);
45
+ int result = size(nums);
46
for (const auto& x : nums) {
47
if (x - 1 >= threshold) {
48
continue;
@@ -51,18 +52,14 @@ class Solution {
51
52
lookup[i - 1] = x - 1;
53
54
}
- uf.union_set(lookup[i - 1], x - 1);
55
+ if (uf.union_set(lookup[i - 1], x - 1)) {
56
+ --result;
57
+ }
58
if (i == x) {
59
break;
60
61
62
- int result = 0;
- for (const auto& x : nums) {
- if (x - 1 >= threshold || uf.find_set(x - 1) == x - 1) {
63
- ++result;
64
- }
65
66
return result;
67
68
};
0 commit comments