leetcode322

public class Solution {
    public int coinChange(int[] coins, int amount) {
        if (amount == 0) return 0;
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for (int i = 1;i <= amount ;i++ ) {
            dp[i] = Integer.MAX_VALUE;
            for(int k :coins) {
                if(i>=k && dp[i-k] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i-k] + 1,dp[i]);
                }
            }
        }
        if(dp[amount]<Integer.MAX_VALUE && dp[amount] > 0) {
            return dp[amount];
        } else {
            return -1;
        }

    }
}

猜你喜欢

转载自www.cnblogs.com/asenyang/p/9787653.html