LC322. 零钱兑换

 代码随想录

class Solution {
    public int coinChange(int[] coins, int amount) {

        int max = Integer.MAX_VALUE;
        int [] dp = new int[amount+1];

        for(int j = 0 ; j < dp.length; j ++){
            dp[j] = max;
        }


        dp[0] = 0;

        for(int i = 0 ; i < coins.length; i ++){
            
            for(int j = coins[i] ; j <= amount ; j++){

                if(dp[j-coins[i]] != max){
                    dp[j] = Math.min(
                        dp[j],
                        dp[j - coins[i]] +1
                    );
                }
            }
        }
        return dp[amount] == max ? -1 : dp[amount] ;
    }
}

你可能感兴趣的:(LeetCode算法题,算法,leetcode,动态规划)