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: pos 0->1 (speed 2), then 1->3 (speed 4). Two accelerations reach position 3 exactly.

Input:target = 1
Output:1
Explanation:

A: Start at pos 0 with speed 1. One A reaches position 1 exactly.

Input:target = 4
Output:5
Explanation:

AARA then more moves needed. Can't reach 4 exactly with pure As. Optimal: 5 instructions.

Constraints

  • 1 ≤ target ≤ 10⁴

Ready to solve this problem?

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