Skip to content

Commit c666d3f

Browse files
authored
Update minimum-numbers-of-function-calls-to-make-target-array.cpp
1 parent 0624829 commit c666d3f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

C++/minimum-numbers-of-function-calls-to-make-target-array.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ class Solution {
66
int minOperations(vector<int>& nums) {
77
int result = 0, max_len = 1;
88
for (const auto& num : nums) {
9-
if (!num) {
10-
continue;
11-
}
129
result += __builtin_popcount(num);
13-
max_len = max(max_len, 32 - __builtin_clz(num));
10+
max_len = max(max_len, bit_length(num));
1411
}
1512
return result + max_len - 1;
1613
}
14+
15+
private:
16+
int bit_length(int x) {
17+
return x != 0 ? 32 - __builtin_clz(x) : 1;
18+
}
1719
};

0 commit comments

Comments
 (0)