-
Notifications
You must be signed in to change notification settings - Fork 3
/
964.cpp
38 lines (36 loc) · 1.16 KB
/
964.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
class Solution {
public:
unordered_map<int, int> mp;
int leastOpsExpressTarget(int x, int target) {
if (mp.find(target) != mp.end()) return mp[target];
if (x == target) return 0;
if (x > target) {
// x/x + x/x + ... + x/x or x - x/x - x/x ...
int res = min(target * 2 - 1, (x - target) * 2);
mp[target] = res;
return mp[target];
}
long long sum = x;
int count = 0;
while (sum < target) {
sum *= x;
count++;
}
if (sum == target) return count;
// sum > target
int res1 = INT_MAX;
int res2 = INT_MAX;
// case1: by minus || target < sum < 2 * target
// x^(count) - recursive result
if (sum - target < target) {
res1 = leastOpsExpressTarget(x, sum - target) + count;
}
// case2: by plus
// x^(count - 1) + recursive result
// minus 1: due to (count - 1)
res2 = leastOpsExpressTarget(x, target - sum / x) + count - 1;
// plus one is +/-
mp[target] = min(res1, res2) + 1;
return mp[target];
}
};