-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathrace_car.java
More file actions
54 lines (48 loc) · 1.74 KB
/
race_car.java
File metadata and controls
54 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Title: Race Car (LeetCode #818)
* Problem:
* The car starts at position 0 and speed +1.
* In each move, you can either:
* - "A": Accelerate -> position += speed; speed *= 2
* - "R": Reverse -> if speed > 0 then speed = -1 else speed = 1
* Find the minimum number of instructions to reach the target position.
*
* Approach:
* Dynamic Programming (DP)
* - dp[i] stores the minimum number of steps required to reach position i.
* - Use bit manipulation to find the smallest k such that (2^k - 1) >= i.
* - Compute based on overshooting or undershooting target.
*
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*
* Author: Mohammed Sufiyan Ahmed
* Date: October 2025
*/
class Solution {
public int racecar(int target) {
int[] dp = new int[target + 1];
for (int i = 1; i <= target; ++i) {
int k = 32 - Integer.numberOfLeadingZeros(i);
// Case 1: If target is exactly at position (2^k - 1)
if (i == (1 << k) - 1) {
dp[i] = k;
continue;
}
// Case 2: Overshoot and then reverse
dp[i] = dp[(1 << k) - 1 - i] + k + 1;
// Case 3: Undershoot and reverse earlier
for (int j = 0; j < k - 1; ++j) {
dp[i] = Math.min(dp[i],
dp[i - (1 << (k - 1)) + (1 << j)] + k - 1 + j + 2);
}
}
return dp[target];
}
// Example test case (for contributors)
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println("Minimum instructions for target=3: " + sol.racecar(3)); // Output: 2
System.out.println("Minimum instructions for target=6: " + sol.racecar(6)); // Output: 5
}
}