Skip to content

Practice Race Car

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#818
LeetCode Problem

Race Car

Problem Statement

Start at position 0 with speed +1 on an unbounded number line. Instruction A adds speed to position and then doubles speed. Instruction R leaves position unchanged and changes a positive speed to -1 or a negative speed to +1. Negative positions are allowed. Return the fewest instructions needed to reach target.

Source: Race Car on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
target = 3
Output
2

# Example 2

Input
target = 6
Output
5

Constraints

1 <= target <= 10^4

Related LeetCode Problems

Practice more problems similar to Race Car.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.