-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0964.cpp
More file actions
36 lines (31 loc) · 732 Bytes
/
0964.cpp
File metadata and controls
36 lines (31 loc) · 732 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
28
29
30
31
32
33
34
35
36
class Solution {
unordered_map<string, int> memo;
int x;
int cost(int i) { return i > 0 ? i : 2; }
int dp(int i, int target) {
string key = to_string(i) + "#" + to_string(target);
if (memo.count(key))
return memo[key];
int res;
if (target == 0) {
res = 0;
} else if (target == 1) {
res = cost(i);
} else if (i >= 39) {
res = target + 1;
} else {
int t = target / x;
int r = target % x;
res =
min(r * cost(i) + dp(i + 1, t), (x - r) * cost(i) + dp(i + 1, t + 1));
}
memo[key] = res;
return res;
}
public:
int leastOpsExpressTarget(int x, int target) {
this->x = x;
memo.clear();
return dp(0, target) - 1;
}
};