Race Car

hard dp bfs math

Problem

A car starts at 0 with speed +1. 'A' doubles speed and moves; 'R' flips direction and resets speed to +/-1. Return the minimum length instruction sequence to reach target.

Inputtarget = 3
Output2
"AA" goes 1 -> 3.

def racecar(target):
    dp = [0, 1, 4] + [0] * (target - 2)
    for t in range(3, target + 1):
        n = t.bit_length()
        if (1 << n) - 1 == t:
            dp[t] = n
            continue
        best = n + 1 + dp[(1 << n) - 1 - t]
        for m in range(n - 1):
            best = min(best, n - 1 + m + 2 + dp[t - (1 << (n-1)) + (1 << m)])
        dp[t] = best
    return dp[target]
var racecar = function(target) {
    const dp = new Array(target + 1).fill(0);
    dp[1] = 1; if (target >= 2) dp[2] = 4;
    for (let t = 3; t <= target; t++) {
        const n = 32 - Math.clz32(t);
        if ((1 << n) - 1 === t) { dp[t] = n; continue; }
        let best = n + 1 + dp[(1 << n) - 1 - t];
        for (let m = 0; m < n - 1; m++) {
            best = Math.min(best, n - 1 + m + 2 + dp[t - (1 << (n-1)) + (1 << m)]);
        }
        dp[t] = best;
    }
    return dp[target];
};
class Solution {
    public int racecar(int target) {
        int[] dp = new int[target + 1];
        dp[1] = 1; if (target >= 2) dp[2] = 4;
        for (int t = 3; t <= target; t++) {
            int n = 32 - Integer.numberOfLeadingZeros(t);
            if ((1 << n) - 1 == t) { dp[t] = n; continue; }
            int best = n + 1 + dp[(1 << n) - 1 - t];
            for (int m = 0; m < n - 1; m++) {
                best = Math.min(best, n - 1 + m + 2 + dp[t - (1 << (n-1)) + (1 << m)]);
            }
            dp[t] = best;
        }
        return dp[target];
    }
}
class Solution {
public:
    int racecar(int target) {
        vector<int> dp(target + 1, 0);
        dp[1] = 1; if (target >= 2) dp[2] = 4;
        for (int t = 3; t <= target; t++) {
            int n = 32 - __builtin_clz(t);
            if ((1 << n) - 1 == t) { dp[t] = n; continue; }
            int best = n + 1 + dp[(1 << n) - 1 - t];
            for (int m = 0; m < n - 1; m++) {
                best = min(best, n - 1 + m + 2 + dp[t - (1 << (n-1)) + (1 << m)]);
            }
            dp[t] = best;
        }
        return dp[target];
    }
};
Time: O(target * log target) Space: O(target)