-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
46 lines (38 loc) · 846 Bytes
/
main.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
36
37
38
39
40
41
42
43
44
45
46
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
int integerReplacement(long n) {
if (n <= 2) {
return 1;
}
if (n % 2 == 0) {
return 1 + integerReplacement(n / 2);
} else {
return 1 + min(integerReplacement(n - 1), integerReplacement(n + 1));
}
}
public:
int integerReplacement(int n) {
return integerReplacement((long) n);
}
};
int main() {
Solution sol;
cout << sol.integerReplacement(8) << endl;
cout << sol.integerReplacement(7) << endl;
cout << sol.integerReplacement(4) << endl;
cout << sol.integerReplacement(2147483647) << endl;
return 0;
}