-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
116 lines (103 loc) · 3.62 KB
/
Copy pathsolution.cpp
File metadata and controls
116 lines (103 loc) · 3.62 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Problem: 818. Race Car
* Difficulty: Hard
* Topics: Dynamic Programming
* LeetCode Link: https://leetcode.com/problems/race-car/
*
* Approach: Dynamic Programming
*
* Key Insight:
* Our speed increases exponentially as 1, 2, 4, 8, ...
* After `n` consecutive 'A' instructions, the distance traveled is `2^n - 1`.
* To reach a target `t`, we can either:
*
* 1. Overshoot the target:
* Accelerate `n` times to reach `2^n - 1` (where `2^n - 1 >= t`).
* Then we reverse ('R') and cover the remaining distance backward.
* The remaining distance is `(2^n - 1) - t`.
* Cost: n (forward) + 1 (reverse) + dp[(2^n - 1) - t].
*
* 2. Undershoot the target:
* Accelerate `k` times (where `k < n`) reaching `2^k - 1 < t`.
* Reverse ('R') and accelerate backward `j` times (where `j < k`), covering `2^j - 1`.
* Reverse again ('R') to face forward.
* Our net position is `(2^k - 1) - (2^j - 1)`.
* The remaining distance to target is `t - ((2^k - 1) - (2^j - 1))`.
* Cost: k (forward) + 1 (reverse) + j (backward) + 1 (reverse) + dp[remaining].
*
* Since both `(2^n - 1) - t` and the undershoot `remaining` distance are STRICTLY
* less than `t`, we can build the answer iteratively using a DP array from 1 to `target`.
*
* Time Complexity: O(T * log^2(T)) where T is the target.
* Space Complexity: O(T) for the DP array.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
class Solution {
public:
int racecar(int target) {
// dp[t] stores the minimum instructions to reach exactly distance t with speed +1
vector<int> dp(target + 1, 1e9);
dp[0] = 0;
for (int t = 1; t <= target; ++t) {
// Find the minimum n such that 2^n - 1 >= t
int n = 1;
while ((1 << n) - 1 < t) {
n++;
}
// If we can reach it exactly
if ((1 << n) - 1 == t) {
dp[t] = n;
continue;
}
// Option 1: Overshoot and go back
// Go forward n steps, then Reverse
dp[t] = min(dp[t], n + 1 + dp[(1 << n) - 1 - t]);
// Option 2: Undershoot, reverse to burn speed/position, reverse again and continue
// We can take k forward steps (k < n)
for (int k = 1; k < n; ++k) {
// Then reverse and take j backward steps (j < k)
for (int j = 0; j < k; ++j) {
int remaining_dist = t - ((1 << k) - 1) + ((1 << j) - 1);
dp[t] = min(dp[t], k + 1 + j + 1 + dp[remaining_dist]);
}
}
}
return dp[target];
}
};
// ==========================================
// Local Test Runner (Guarded for LeetCode Submission)
// ==========================================
#ifdef LOCAL_TEST
int main() {
Solution solver;
// Test Case 1
{
int target = 3;
int res = solver.racecar(target);
assert(res == 2); // "AA"
cout << "Test 1 Passed! Result: " << res << endl;
}
// Test Case 2
{
int target = 6;
int res = solver.racecar(target);
assert(res == 5); // "AAARA"
cout << "Test 2 Passed! Result: " << res << endl;
}
// Test Case 3
{
int target = 5;
int res = solver.racecar(target);
// Expected length is 7: AARARAA
assert(res == 7);
cout << "Test 3 Passed! Result: " << res << endl;
}
cout << "\nAll test cases passed successfully!" << endl;
return 0;
}
#endif