Skip to main content
Back to problems
#2139
Medium Algorithms

Minimum moves to reach target score

Math Greedy
52.3% acceptance
Feb 25, 2026
1086
28
You are playing a game with integers. You start with the integer 1 and you want to reach the integer target. In one move, you can either: Increment the current integer by one (i.e., x = x + 1). Double the current integer (i.e., x = 2 * x). You can use the increment operation any number of times, however, you can only use the double operation at most maxDoubles times. Given the two integers target and maxDoubles, return the minimum number of moves needed to reach target starting with 1.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_moves(target: i32, max_doubles: i32) -> i32 {
    let mut target = target;
    let mut max_doubles = max_doubles;
    let mut moves = 0;
    // Work backwards from target to 1
    while target > 1 && max_doubles > 0 {
      if target % 2 == 1 {
        // Must decrement before halving
        target -= 1;
        moves += 1;
      } else {
        // Halve (reverse of doubling)
        target /= 2;
        moves += 1;
        max_doubles -= 1;
      }
    }
    // All remaining moves are increments from 1
    moves + target - 1
  }
}