LeetCode.322:零钱兑换(DP)

class Solution {
    public int coinChange(int[] coins, int amount) {
        
        Arrays.sort(coins);
        int[] dp = new int[amount+1];
        Arrays.fill(dp,amount+1);
        dp[0] = 0;
        int len = coins.length;
        for(int i=coins[0];i<=amount;i++){
            for(int j=0;j<len;j++)
            {
                if(i>=coins[j])
                    dp[i] = Math.min(dp[i],dp[i-coins[j]]+1);
            }
        }


        return dp[amount]==(amount+1)?-1:dp[amount];
    }
}

 

posted @ 2020-10-10 10:11  dlooooo  阅读(79)  评论(0编辑  收藏  举报