Tap here to switch tabs
Problem
Submissions

Problem: Race Car

med
30 min
Try to solve the Race Car problem.

Statement

Your car starts at position 00 with speed 11 on an infinite number line. Given the target position target, return the minimum number of instructions needed for the car to reach that position.

The car accepts two instructions:

  • A accelerates the car. Its position increases by its current speed, then its speed doubles.

  • R reverses the car. Its position remains unchanged, and its speed becomes −1-1 if it was positive or 11 if it was negative.

Implement def racecar(self, target): to return the length of the shortest instruction sequence that moves the car to target.

Constraints:

  • 1≤1 \leq target ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Race Car

med
30 min
Try to solve the Race Car problem.

Statement

Your car starts at position 00 with speed 11 on an infinite number line. Given the target position target, return the minimum number of instructions needed for the car to reach that position.

The car accepts two instructions:

  • A accelerates the car. Its position increases by its current speed, then its speed doubles.

  • R reverses the car. Its position remains unchanged, and its speed becomes −1-1 if it was positive or 11 if it was negative.

Implement def racecar(self, target): to return the length of the shortest instruction sequence that moves the car to target.

Constraints:

  • 1≤1 \leq target ≤104\leq 10^4