-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2571.cpp
More file actions
27 lines (25 loc) · 733 Bytes
/
Copy path2571.cpp
File metadata and controls
27 lines (25 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
int rec(int n, int idx) const {
if (n == 0) return 0;
int res = INT_MAX, steps = 0;
for (int i = idx, mask = 1 << idx; n != 0 && i < 17; i++, mask <<= 1) {
if ((n & mask) == 0) continue;
res = min(res, 1 + rec(n - mask, i + 1) + steps);
n += mask, steps++;
}
return res;
}
public:
int minOperations(int n) const { return rec(n, 0); }
};
// Greedy
class Solution {
public:
int minOperations(unsigned n) const {
int res = 0;
for (unsigned i = 0, mask = 1; i < 17; i++, mask <<= 1) {
if (popcount(n + mask) < popcount(n)) res++, n += mask;
}
return res + popcount(n);
}
};