2139. Minimum Moves to Reach Target Score

greedy

T : O(n), n is maxDoubles

S: O(1)

class Solution {
    public int minMoves(int target, int maxDoubles) {
        if (target == 1) {
            return 0;
        }
        int count = 0;
        for (int i = 0; i < maxDoubles; i++) {
            int remain = target%2;
            if (remain != 0) {
                count++;
            }
            target /= 2;
            count++;
            if (target == 1) {
                break;
            }
        }
        count += target - 1;
        return count;
    }
}

/*
Given the two integers target and maxDoubles, return the minimum number of moves needed to reach target starting with 1.

10/2 = 5
            5/2 =2
            
            d
            d
            1
            d
            
19/2 = 9, reamin 1
9/2 = 4, reamin 1  max use out
4-1 = 3

*/

Last updated