greedy January 16, 2023

Minimum moves to reach target score

Time O(log(n)) Space O(1) Open original problem

We should use double action as late as possible, as many times as possible. We can use a greedy approach to solve this problem.

class Solution:
    def minMoves(self, target: int, maxDoubles: int) -> int:
        res = 0
        while target > 1 and maxDoubles > 0:
            res += 1 + target % 2
            maxDoubles -= 1
            target //= 2
        return target - 1 + res

Time complexity: O(log(n)), n is the target.
Space complexity: O(1)