> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Race Car Python Solution with Tests

> Tested Python solution for LeetCode 818 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 818, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming), Heuristic Search, A\* Search. [View on LeetCode](https://leetcode.com/problems/race-car/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 818   # by problem number
lcpy gen -s race_car   # by problem name
```

## Problem

Your car starts at position `0` and speed `+1` on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions `'A'` (accelerate) and `'R'` (reverse):

* When you get an instruction `'A'`, your car does the following:
  * `position += speed`
  * `speed *= 2`
* When you get an instruction `'R'`, your car does the following:
  * If your speed is positive then `speed = -1`
  * otherwise `speed = 1`
    Your position stays the same.

For example, after commands `'AAR'`, your car goes to positions `0 --> 1 --> 3 --> 3`, and your speed goes to `1 --> 2 --> 4 --> -1`.

Given a target position `target`, return *the length of the shortest sequence of instructions to get there*.

### Examples

```
Input: target = 3
Output: 2
```

**Explanation:** The shortest instruction sequence is `'AA'`. Your position goes from `0 --> 1 --> 3`.

```
Input: target = 6
Output: 5
```

**Explanation:** The shortest instruction sequence is `'AAARA'`. Your position goes from `0 --> 1 --> 3 --> 7 --> 7 --> 6`.

### Constraints

* 1 \<= target \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/race_car/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/race_car/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(target * log(target))
    # Space: O(target)
    def racecar(self, target: int) -> int:
        dp = [0] * (target + 1)
        for t in range(1, target + 1):
            k = t.bit_length()
            if t == 2**k - 1:
                dp[t] = k
                continue
            best = k + 1 + dp[2**k - 1 - t]
            for j in range(k - 1):
                nxt = t - 2 ** (k - 1) + 2**j
                best = min(best, k + j + 1 + dp[nxt])
            dp[t] = best
        return dp[target]
```

## Complexity

| Time | Space |
| - | - |
| O(target \* log(target)) | O(target) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.