Leetcode
Dynamic Programming
656 Coin Path

656. Coin Path

1. Question

Given an arrayA(index starts at1) consisting of N integers: A1, A2, ..., AN and an integerB. The integerBdenotes that from any place (suppose the index isi) in the arrayA, you can jump to any one of the place in the arrayAindexedi+1,i+2, …,i+Bif this place can be jumped to. Also, if you step on the indexi, you have to pay Ai coins. If Ai is -1, it means you can’t jump to the place indexediin the array.
Now, you start from the place indexed1in the arrayA, and your aim is to reach the place indexedNusing the minimum coins. You need to return the path of indexes (starting from 1 to N) in the array you should take to get to the place indexedNusing minimum coins.
If there are multiple paths with the same cost, return the lexicographically smallest such path.
If it's not possible to reach the place indexed N then you need to return an empty array.
Example 1:
1
Input: [1,2,4,-1,2], 2
2
3
Output: [1,3,5]
Copied!
Example 2:
1
Input: [1,2,4,-1,2], 1
2
3
Output: []
Copied!
Note:
  1. 1.
    Path Pa1, Pa2, ..., Pan is lexicographically smaller than Pb1, Pb2, ..., Pbm, if and only if at the firsti
    where Pai and Pbi differ, Pai < Pbi; when no such iexists, then n<m.
  2. 2.
    A1 >= 0. A2, ..., AN (if exist) will in the range of [-1, 100].
  3. 3.
    Length of A is in the range of [1, 1000].
  4. 4.
    B is in the range of [1, 100].

2. Implementation

(1) DP
思路: 从后往前扫一遍数组,用两个数组dp和preIndex分别记录以下信息,dp[i]表示从开头到位置i的min cost, preIndex记录在min cost path中位置i前的一个位置
1
class Solution {
2
public List<Integer> cheapestJump(int[] A, int B) {
3
List<Integer> res = new ArrayList<>();
4
5
if (A == null || A.length == 0 || A[A.length - 1] == -1) {
6
return res;
7
}
8
9
int n = A.length;
10
// dp[i] is the min cost to reach position i from the start
11
int[] dp = new int[n];
12
Arrays.fill(dp, Integer.MAX_VALUE);
13
dp[n - 1] = A[n - 1];
14
15
// preIndex[i] store the previous index of i in the min cost path
16
int[] preIndex = new int[n];
17
Arrays.fill(preIndex, -1);
18
19
for (int i = n - 2; i >= 0; i--) {
20
// Ignore position we cannot reach
21
if (A[i] == -1) continue;
22
for (int j = i + 1; j <= Math.min(i + B, n - 1); j++) {
23
if (dp[j] == Integer.MAX_VALUE) continue;
24
25
if (A[i] + dp[j] < dp[i]) {
26
dp[i] = A[i] + dp[j];
27
preIndex[i] = j;
28
}
29
}
30
}
31
32
// Cannot reach A[n - 1] from A[0]
33
if (dp[0] == Integer.MAX_VALUE) {
34
return res;
35
}
36
37
for (int index = 0; index != -1; index = preIndex[index]) {
38
res.add(index + 1);
39
}
40
return res;
41
}
42
}
Copied!

3. Time & Space Complexity

时间复杂度O(n^2), 空间复杂度O(n)