Hard · Dynamic Programming

Race Car

A car starts at position 0 with speed +1; instruction A does position += speed then speed *= 2, and R leaves the position and sets speed to −1 if it was positive, else +1. Given target, return the length of the shortest instruction string that ends with the car at position target.

Examples

Example 1

{
  "target": 6
}

Output: 5 instructions

Example 2

{
  "target": 3
}

Output: 2 instructions

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Dynamic Programming problems