LeetCode 656: Coin Path

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 656: Coin Path相关的知识,希望对你有一定的参考价值。

Note:

1. Find each steps lowest cost.

2. Find whether it could reach to end or not.

class Solution {
    public List<Integer> cheapestJump(int[] A, int B) {
        int[] next = new int[A.length];
        long[] dp = new long[A.length];
        Arrays.fill(next, -1);
        List<Integer> result = new ArrayList<>();
        for (int i = A.length - 2; i >= 0; i--) {
            long minCost = Integer.MAX_VALUE;
            for (int j = i + 1; j <= i + B && j < A.length; j++) {
                if (A[j] >= 0) {
                    long cost = A[i] + dp[j];
                    if (cost < minCost) {
                        minCost = cost;
                        next[i] = j;
                    }
                }
            }
            dp[i] = minCost;
        }
        
        int i = 0;
        for (; i < A.length && next[i] > 0; i = next[i]) {
            result.add(i + 1);
        }
        result.add(A.length);
        return i == A.length - 1 && A[i] >= 0 ? result : new ArrayList<>();
    }
}

 

以上是关于LeetCode 656: Coin Path的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 656. Coin Path 硬币路径

656. Coin Path

[LeetCode] Coin Path 硬币路径

Leetcode322. coin-change

LeetCode Coin Change

[LeetCode] Coin Change