322.零钱兑换

class Solution {
    public int coinChange(int[] coins, int amount) {
        int max = Integer.MAX_VALUE;
        int[] dp = new int[amount + 1];
        // 题目要取得最小,那么初始化要为max,和T279一样
        for(int i = 0; i <= amount; i++) dp[i] = max;

        dp[0] = 0;
        // 遍历背包
        for(int i = 1; i <= amount; i++){
            // 遍历物品
            for(int j = 0; j < coins.length; j++){
                if(i >= coins[j] && dp[i - coins[j]] != max){
                    dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
                } 
            }
        }
        return dp[amount] == max ? -1 : dp[amount];
    }
}
上一篇:vue+element-UI 实现的假进度条


下一篇:【力扣-动态规划入门】【第 20 天】518. 零钱兑换 II