-
Notifications
You must be signed in to change notification settings - Fork 3
/
656.cpp
29 lines (29 loc) · 993 Bytes
/
656.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
class Solution {
public:
vector<int> cheapestJump(vector<int>& coins, int maxJump) {
int n = coins.size();
vector<int> paid(n, INT_MAX);
vector<int> prev(n, -1);
vector<int> counts(n, 0);
paid[0] = 0;
for (int i = 1; i < n; ++i) {
if (coins[i] == -1) continue;
for (int j = max(0, i - maxJump); j < i; ++j) {
if (paid[j] == INT_MAX) continue;
int newPaid = paid[j] + coins[i];
if (newPaid < paid[i] || (newPaid == paid[i] && counts[i] < counts[j] + 1)) {
paid[i] = newPaid;
prev[i] = j;
counts[i] = counts[j] + 1;
}
}
}
if (paid[n - 1] == INT_MAX) return {};
vector<int> res;
for (int curr = n - 1; curr != -1; curr = prev[curr]) {
res.push_back(curr + 1);
}
reverse(res.begin(), res.end());
return res;
}
};