We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cf4f922 commit 306b4dfCopy full SHA for 306b4df
scripts/algorithms/M/Minimum Moves to Reach Target Score/Minimum Moves to Reach Target Score.java
@@ -1,19 +1,17 @@
1
class Solution {
2
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--;
+ int ans = 0;
+ for(int i=0;i<maxDoubles;i++){
+ if(target==1)break;
+
+ if(target%2==0){
+ ans+=1;
+ target=(target)/2;
+ }else{
+ ans+=2;
+ target=(target-1)/2;
13
}
14
- steps++;
15
16
-
17
- return steps;
+ return ans+target-1;
18
19
-}
+}
0 commit comments