Description

Your car starts at position 0 and speed 1. You can accelerate (A) or reverse (R). Return the minimum number of instructions to reach target position.

Examples

Input:target = 3
Output:2
Explanation:

AA: position becomes 3.

Input:target = 1
Output:1
Explanation:

A: position becomes 1. Starting at position 0 with speed 1, one acceleration instruction moves the car to position 1.

Input:target = 4
Output:4
Explanation:

Sequence AAARA: A moves to position 1 (speed 2), A moves to position 3 (speed 4), A moves to position 7 (speed 8), R reverses (speed -1), A moves to position 6. The minimum number of instructions to reach position 6 is 5.

Constraints

  • 1 ≤ target ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!