dp
and next
arrays of size n
with dp[n-1] = coins[n-1]
and next[n-1] = -1
.i
, initialize min_cost
to infinity
and min_index
to -1
.i+1
to i+maxJump
(inclusive) and within the bounds of the array.j
, if coins[j]
is not -1
and dp[j] + coins[i]
is less than min_cost
, update min_cost
and min_index
.dp[i]
to min_cost
and next[i]
to min_index
.dp
and next
, initialize the result list res
with the first index 1
.next
to construct the path and add each index to res
.next[i]
is -1
, return an empty list as it's not possible to reach the end.res
list.