-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-time-to-finish-the-race.cpp
30 lines (28 loc) · 1.25 KB
/
minimum-time-to-finish-the-race.cpp
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
// Time: O((n + l) * logc)
// Space: O(n + l + logc)
// greedy, dp
class Solution {
public:
int minimumFinishTime(vector<vector<int>>& tires, int changeTime, int numLaps) {
static const int INF = numeric_limits<int>::max();
const auto& ceil_log2 = [](const auto& x) {
return std::__lg(x - 1) + 1;
};
vector<int64_t> dp(ceil_log2(changeTime + 1), INF); // dp[i]: min time to complete i+1 laps without changing a tire
for (int i = 0; i < size(tires); ++i) {
const int f = tires[i][0], r = tires[i][1];
for (int64_t curr = f, total = f, cnt = 0;
curr < changeTime + f;
curr *= r, total += curr, ++cnt) { // at worst (f, r) = (1, 2) => 2^(cnt-1) < changeTime+1 => cnt < ceil(log2(changeTime+1))
dp[cnt] = min(dp[cnt], total);
}
}
vector<int64_t> dp2(numLaps, INF); // dp2[i]: min time to complete i+1 laps with changing zero or more tires
for (int i = 0; i < numLaps; ++i) {
for (int j = 0; j < min(i + 1, static_cast<int>(size(dp))); ++j) {
dp2[i] = min(dp2[i], (i - j - 1 >= 0 ? dp2[i - j - 1] + changeTime : 0) + dp[j]);
}
}
return dp2.back();
}
};