forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path656.Coin-Path.cpp
44 lines (38 loc) · 989 Bytes
/
656.Coin-Path.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
vector<int> cheapestJump(vector<int>& A, int B)
{
int n=A.size();
vector<int>dp(n,INT_MAX);
vector<int>path(n,-1);
dp[n-1]=A[n-1];
for (int i=n-1; i>=0; i--)
{
for (int k=1; k<=B; k++)
{
if (i+k>=A.size())
continue;
if (A[i+k]==-1)
continue;
if (dp[i+k]+A[i]<dp[i])
{
dp[i]=dp[i+k]+A[i];
path[i]=i+k;
}
}
}
vector<int>result;
int i=0;
while (i<A.size())
{
result.push_back(i+1);
if (i==A.size()-1)
break;
if (path[i]==-1)
return {};
else
i=path[i];
}
return result;
}
};