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

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Dynamic Programming problems