You are playing a game with integers. You start with a score of 1 and you want to reach a score of target.
In one move, you can either:
-
increase your score by
1
, or
-
double your score (you can use this operation at most
max_doubles
times).
Given the two integers target and max_doubles, return the minimum number of moves needed to reach target starting from a score of 1.
Function Signature
def min_moves(target: int, max_doubles: int) -> int:
Rules
-
Increments are unlimited. Doublings are limited to
max_doubles
, and you do not have to use all of them.
-
The score never decreases, so the sequence must land exactly on
target
.
-
When
target
is
1
the answer is
0
, whatever
max_doubles
is.
Constraints
-
1 <= target <= 10^9
-
0 <= max_doubles <= 100
Examples
Input: target = 5, max_doubles = 0
Output: 4
With no doubles allowed, the only option is to increment 4 times: 1 -> 2 -> 3 -> 4 -> 5.
Input: target = 19, max_doubles = 2
Output: 7
One optimal sequence uses both doubles: three increments to 4, double to 8, increment to 9, double to 18, increment to 19. That is 3 + 1 + 1 + 1 + 1 = 7 moves.
Input: target = 10, max_doubles = 4
Output: 4
The path 1 -> 2 -> 4 -> 5 -> 10 takes 4 moves and stays within the four allowed doubles. No 3-move path exists even with unlimited doubles, because three moves can only reach 4, 5, 6 or 8.