Skip to content

Commit 306b4df

Browse files
committed
Runtime 0 ms (Top 100.0%) | Memory 39.0 MB (Top 80.93%)
1 parent cf4f922 commit 306b4df

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
class Solution {
22
public int minMoves(int target, int maxDoubles) {
3-
int steps = 0;
4-
while(target > 1) {
5-
if (target % 2 == 0 && maxDoubles > 0) {
6-
target /= 2;
7-
maxDoubles--;
8-
} else if (maxDoubles == 0) {
9-
steps += target - 1;
10-
break;
11-
} else {
12-
target--;
3+
int ans = 0;
4+
for(int i=0;i<maxDoubles;i++){
5+
if(target==1)break;
6+
7+
if(target%2==0){
8+
ans+=1;
9+
target=(target)/2;
10+
}else{
11+
ans+=2;
12+
target=(target-1)/2;
1313
}
14-
steps++;
1514
}
16-
17-
return steps;
15+
return ans+target-1;
1816
}
19-
}
17+
}

0 commit comments

Comments
 (0)