836. Race Car
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum number of instructions ('A' for accelerate, 'R' for reverse) to move a car from position 0 to a target position on an infinite number line. The car's state is defined by its position and speed. Initially, position is 0 and speed is +1.
Solution
Race Car in Python
class Solution:
def racecar(self, target: int) -> int:
# dp[i] stores the minimum steps to reach position i.
dp = [0] * (target + 1)
for i in range(1, target + 1):
# k is the smallest number of 'A's to reach or pass position i.
# A sequence of k 'A's reaches position 2^k - 1.
# We need 2^(k-1) <= i < 2^k.
# k can be found by the bit length of i.
k = i.bit_length()
# Case 1: Exactly k 'A's to reach i.
if (1 << k) - 1 == i:
dp[i] = k
continue
# Case 2: Overshoot. Use k 'A's to pass i, then reverse and come back.
# Position becomes (1<<k) - 1. Steps: k (A's) + 1 (R).
# The remaining distance to cover backward is ((1<<k) - 1) - i.
dp[i] = k + 1 + dp[(1 << k) - 1 - i]
# Case 3: Undershoot. Use k-1 'A's, then reverse, move backward j 'A's,
# reverse again, and then move forward.
k_under = k - 1
pos_before = (1 << k_under) - 1
for j in range(k_under):
# After k-1 'A's, 1 'R', and j 'A's (backward):
# Current position: pos_before - ((1<<j) - 1)
# Total steps for this maneuver: k_under (A's) + 1 (R) + j (A's) + 1 (R)
# Remaining distance to cover is i - current_position.
current_pos = pos_before - ((1 << j) - 1)
cost = k_under + 1 + j + 1 + dp[i - current_pos]
dp[i] = min(dp[i], cost)
return dp[target]
Race Car in Java
class Solution {
public int racecar(int target) {
// dp[i] stores the minimum steps to reach position i.
int[] dp = new int[target + 1];
for (int i = 1; i <= target; i++) {
// k is the smallest number of 'A's to reach or pass position i.
// A sequence of k 'A's reaches position 2^k - 1.
// We need 2^(k-1) <= i < 2^k.
// k = floor(log2(i)) + 1, which can be computed with bit manipulation.
int k = 32 - Integer.numberOfLeadingZeros(i);
// Case 1: Exactly k 'A's to reach i.
if ((1 << k) - 1 == i) {
dp[i] = k;
continue;
}
// Case 2: Overshoot. Use k 'A's to pass i, then reverse and come back.
// Position becomes (1<<k) - 1. Steps: k (A's) + 1 (R).
// The remaining distance to cover backward is ((1<<k) - 1) - i.
dp[i] = k + 1 + dp[(1 << k) - 1 - i];
// Case 3: Undershoot. Use k-1 'A's, then reverse, move backward j steps,
// reverse again, and then move forward.
int k_under = k - 1;
int pos_before = (1 << k_under) - 1;
for (int j = 0; j < k_under; j++) {
// After k-1 'A's, 1 'R', and j 'A's (backward):
// Current position: pos_before - ((1<<j) - 1)
// Total steps for this maneuver: k_under (A's) + 1 (R) + j (A's) + 1 (R)
// Remaining distance to cover is i - current_position.
int current_pos = pos_before - ((1 << j) - 1);
int cost = k_under + 1 + j + 1 + dp[i - current_pos];
dp[i] = Math.min(dp[i], cost);
}
}
return dp[target];
}
}
Race Car in C++
class Solution {
public:
int racecar(int target) {
// dp[i] stores the minimum steps to reach position i.
vector<int> dp(target + 1, 0);
for (int i = 1; i <= target; ++i) {
// k is the smallest number of 'A's to reach or pass position i.
// A sequence of k 'A's reaches position 2^k - 1.
// We need 2^(k-1) <= i < 2^k.
// k = floor(log2(i)) + 1.
int k = 32 - __builtin_clz(i);
// Case 1: Exactly k 'A's to reach i.
if ((1 << k) - 1 == i) {
dp[i] = k;
continue;
}
// Case 2: Overshoot. Use k 'A's to pass i, then reverse and come back.
// Position becomes (1<<k) - 1. Steps: k (A's) + 1 (R).
// The remaining distance to cover backward is ((1<<k) - 1) - i.
dp[i] = k + 1 + dp[(1 << k) - 1 - i];
// Case 3: Undershoot. Use k-1 'A's, then reverse, move backward j steps,
// reverse again, and then move forward.
int k_under = k - 1;
int pos_before = (1 << k_under) - 1;
for (int j = 0; j < k_under; ++j) {
// After k-1 'A's, 1 'R', and j 'A's (backward):
// Current position: pos_before - ((1<<j) - 1)
// Total steps for this maneuver: k_under (A's) + 1 (R) + j (A's) + 1 (R)
// Remaining distance to cover is i - current_position.
int current_pos = pos_before - ((1 << j) - 1);
int cost = k_under + 1 + j + 1 + dp[i - current_pos];
dp[i] = min(dp[i], cost);
}
}
return dp[target];
}
};
Race Car in TypeScript
function racecar(target: number): number {
// dp[i] stores the minimum steps to reach position i.
const dp: number[] = new Array(target + 1).fill(0);
for (let i = 1; i <= target; i++) {
// k is the smallest number of 'A's to reach or pass position i.
// A sequence of k 'A's reaches position 2^k - 1.
// We need 2^(k-1) <= i < 2^k.
// k = floor(log2(i)) + 1, which can be found by length of binary string.
const k = Math.floor(Math.log2(i)) + 1;
// Case 1: Exactly k 'A's to reach i.
if ((1 << k) - 1 === i) {
dp[i] = k;
continue;
}
// Case 2: Overshoot. Use k 'A's to pass i, then reverse and come back.
// Position becomes (1<<k) - 1. Steps: k (A's) + 1 (R).
// The remaining distance to cover backward is ((1<<k) - 1) - i.
dp[i] = k + 1 + dp[(1 << k) - 1 - i];
// Case 3: Undershoot. Use k-1 'A's, then reverse, move backward j steps,
// reverse again, and then move forward.
const k_under = k - 1;
const pos_before = (1 << k_under) - 1;
for (let j = 0; j < k_under; j++) {
// After k-1 'A's, 1 'R', and j 'A's (backward):
// Current position: pos_before - ((1<<j) - 1)
// Total steps for this maneuver: k_under (A's) + 1 (R) + j (A's) + 1 (R)
// Remaining distance to cover is i - current_position.
const current_pos = pos_before - ((1 << j) - 1);
const cost = k_under + 1 + j + 1 + dp[i - current_pos];
dp[i] = Math.min(dp[i], cost);
}
}
return dp[target];
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.