Coin Path Problem
Description
LeetCode Problem 656.
You are given an integer array coins (1-indexed) of length n and an integer maxJump. You can jump to any index i of the array coins if coins[i] != -1 and you have to pay coins[i] when you visit index i. In addition to that, if you are currently at index i, you can only jump to any index i + k where i + k <= n and k is a value in the range [1, maxJump].
You are initially positioned at index 1 (coins[1] is not -1). You want to find the path that reaches index n with the minimum cost.
Return an integer array of the indices that you will visit in order so that you can reach index n with the minimum cost. If there are multiple paths with the same cost, return the lexicographically smallest such path. If it is not possible to reach index n, return an empty array.
A path p1 = [Pa_1, Pa_2, ..., Pa_x] of length x is lexicographically smaller than p2 = [Pb_1, Pb_2, ..., Pb_x] of length y, if and only if at the first j where Pa_j and Pb_j differ, Pa_j < Pb_j; when no such j exists, then x < y.
Example 1:
Input: coins = [1,2,4,-1,2], maxJump = 2
Output: [1,3,5]
Example 2:
Input: coins = [1,2,4,-1,2], maxJump = 1
Output: []
Constraints:
1 <= coins.length <= 1000-1 <= coins[i] <= 100coins[1] != -11 <= maxJump <= 100
Solution
Approach: Dynamic Programming from the End
Let cost[i] be the cheapest total for a path that starts at index i and reaches the last index, counting coins[i] itself. We fill it from right to left:
cost[n - 1] = coins[n - 1], unless the last index is-1(then nothing is reachable).- For an index with
coins[i] == -1,cost[i]stays infinite: we can never stand there. - Otherwise
cost[i] = coins[i] + min(cost[j])over the nextmaxJumpindicesj.
If cost[0] is infinite, return [].
Computing the costs backwards (and not forwards from index 1) makes the tie-break easy. We know the cheapest total from every index, so we can build the path from the front. We start with need = cost[0], and scan the indices from left to right. Whenever cost[i] == need, we take i, so the cheapest total is still reachable from there, and subtract coins[i] from need. Taking the left-most index that still works at every step gives the lexicographically smallest cheapest path. The next index we take is always within maxJump of the current one: the best next step j already satisfies cost[j] == need, so the scan stops at j or earlier.
A path with lower indices can be longer. For coins = [0,0,0,0], maxJump = 2, the answer is [1,2,3,4], not [1,3,4], because 2 < 3 at the second position.
Complexity: O(n · maxJump) time and O(n) extra space.
class Solution:
def cheapestJump(self, coins: List[int], maxJump: int) -> List[int]:
n = len(coins)
INF = float("inf")
# cost[i] = cheapest cost of a path from index i to the last index (including coins[i]).
cost = [INF] * n
if coins[-1] != -1:
cost[-1] = coins[-1]
for i in range(n - 2, -1, -1):
if coins[i] == -1:
continue
for j in range(i + 1, min(n, i + maxJump + 1)):
cost[i] = min(cost[i], coins[i] + cost[j])
if cost[0] == INF:
return []
# Walk forward, always taking the left-most index that still allows the cheapest total.
path = []
need = cost[0]
for i in range(n):
if cost[i] == need:
path.append(i + 1)
need -= coins[i]
return path