-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmerge-operations-for-minimum-travel-time.cpp
More file actions
33 lines (32 loc) · 1.15 KB
/
Copy pathmerge-operations-for-minimum-travel-time.cpp
File metadata and controls
33 lines (32 loc) · 1.15 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
// Time: O((n - k) * k^3)
// Space: O(k^2)
// prefix sum, dp
class Solution {
public:
int minTravelTime(int l, int n, int k, vector<int>& position, vector<int>& time) {
static const int INF = numeric_limits<int>::max();
vector<int> prefix(n + 1);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + time[i];
}
unordered_map<int, unordered_map<int, int>> dp;
dp[0][time[0]] = 0;
for (int cnt = 2; cnt <= n - k; ++cnt) {
unordered_map<int, unordered_map<int, int>> new_dp;
for (int i = cnt - 1; i < (cnt - 1) + (k + 1); ++i) {
for (int j = cnt - 2; j < i; ++j) {
for (const auto& [t, c] : dp[j]) {
const int nt = prefix[i + 1] - prefix[j + 1];
new_dp[i][nt] = min(new_dp[i].count(nt) ? new_dp[i][nt] : INF, (position[i] - position[j]) * t + c);
}
}
}
dp = move(new_dp);
}
int result = INF;
for (const auto& [_, c] : dp[n - 1]) {
result = min(result, c);
}
return result;
}
};