-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path964.java
More file actions
53 lines (50 loc) · 1.75 KB
/
964.java
File metadata and controls
53 lines (50 loc) · 1.75 KB
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
47
48
49
50
51
52
53
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int leastOpsExpressTarget(int x, int target) {
int pos = 0; // the number of operations needed to get y%(x^(k+1))
int neg = 0; // the number of operations needed to get x^(k+1) - y%(x^(k+1))
int k = 0; // the power
int pos2;
int neg2;
int cur;
while(target > 0) {
cur = target % x; // remainder, 余数
target /= x; // quotient, 商
if(k > 0) {
pos2 = Math.min(cur * k + pos, (cur + 1) * k + neg);
neg2 = Math.min((x - cur) * k + pos, (x - cur - 1) * k + neg);
pos = pos2;
neg = neg2;
} else {
pos = 2 * cur;
neg = 2 * (x - cur);
}
k++;
}
return Math.min(pos, k + neg) - 1;
}
}
__________________________________________________________________________________________________
sample 32072 kb submission
class Solution {
public int leastOpsExpressTarget(int x, int target) {
int pos = 0, neg = 0, k = 0;
while(target > 0) {
int r = target % x;
target /= x;
if(k > 0) {
int p = Math.min(r*k + pos, (r+1)*k + neg);
int n = Math.min((x-r)*k + pos, (x-r-1)*k+neg);
pos = p;
neg = n;
} else {
pos = r * 2;
neg = (x-r)*2;
}
k++;
}
return Math.min(pos, k + neg) - 1;
}
}
__________________________________________________________________________________________________