-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathInteger_Replacement.cpp
35 lines (31 loc) · 1.03 KB
/
Integer_Replacement.cpp
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
28
29
30
31
32
33
34
35
class Solution {
public:
int integerReplacement(int n) {
if(1 == n) return 0;
return 1 + ((n & 1) ? 1 + min( integerReplacement(n >> 1), integerReplacement((n >> 1) + 1) ) : integerReplacement(n >> 1));
}
};
// Memorization
class Solution {
int integerReplacement(int n, unordered_map<int, int>& dp) {
if(1 == n) return 0;
if(dp.find(n) != dp.end()) return dp[n];
return dp[n] = 1 + ((n & 1) ? 1 + min( integerReplacement(n >> 1, dp), integerReplacement((n >> 1) + 1, dp) ) : integerReplacement(n >> 1, dp));
}
public:
int integerReplacement(int n) {
unordered_map<int, int> dp;
return integerReplacement(n, dp);
}
};
// Bit manipulation
class Solution {
public:
int integerReplacement(int n) {
if(n <= 3) return (n - 1);
if(n & 1) {
return 2 + (__builtin_popcount(n >> 1) < __builtin_popcount((n >> 1) + 1) ? integerReplacement(n >> 1) : integerReplacement((n >> 1) + 1));
}
return 1 + integerReplacement(n >> 1);
}
};