322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

Note:
You may assume that you have an infinite number of each kind of coin.

題意:

給定一個硬幣數組還有目標金額,輸出一個最少可以組成該金額的硬幣組合,例如:

coins = [1,2,5], amount = 11

reutrn 3 (11 = 5 + 5 + 1) 11元可以用兩個5元還有1個1元組成

若無法組成該金額,則輸出-1,例如:

coits = [2], amount = 3

return -1

題解:

我們可以利用動態規劃來解這一道題,d[i]代表用金額i最少用多少硬幣所組成,動態規劃公式如下:

d[i] = 0 if i == 0

d[i] = Math.min(d[i - coin[j]] + 1, d[i]) if i > 0 and d[coin[j]] != Integer.MAX_VALUE and j = 0~coin.length

例如:

    amount = 12
    ex: 2,5
    
    i = 0 d[0] = 0
    i = 1 d[1] = Integer.MAX_VALUE
    i = 2 d[2] = d[2 - coin[2]] + 1 = d[2 - 2] + 1 = 1
    i = 3 d[3] = Integer.MAX_VALUE
    i = 4 d[4] = d[4 - coin[2]] + 1 = d[4 - 2] + 1 = 2
    i = 5 d[5] = d[5 - coin[5]] + 1 = d[5 - 5] + 1 = 1
    i = 6 d[6] = d[6 - coin[6]] + 1 = d[6 - 2] + 1 = 3
    i = 7 d[7] = Integer.MAX_VALUE
    i = 8 d[8] = d[8 - coin[2]] + 1 = d[6 - 2] + 1 = 4
    i = 9 d[9] = Integer.MAX_VALUE
    i = 10 d[2] = d[10 - coin[5]] + 1 = d[10 - 5] + 1 = 2
    i = 11 d[11] = Integer.MAX_VALUE
    i = 12 d[12] = d[12 - coin[10]] + 1 = d[12 - 10] + 1 = 3

package LeetCode.Medium;
public class CoinChange {
    /*
我們可以利用動態規劃來解這一道題,d[i]代表用金額i最少用多少硬幣所組成,動態規劃公式如下:
d[i] = 0 if i == 0
d[i] = Math.min(d[i - coin[j]] + 1, d[i]) if i > 0 and d[coin[j]] != Integer.MAX_VALUE
例如:
    amount = 12
    ex: 2,5
    
    i = 0 d[0] = 0
    i = 1 d[1] = Integer.MAX_VALUE
    i = 2 d[2] = d[2 - coin[2]] + 1 = d[2 - 2] + 1 = 1
    i = 3 d[3] = Integer.MAX_VALUE
    i = 4 d[4] = d[4 - coin[2]] + 1 = d[4 - 2] + 1 = 2
    i = 5 d[5] = d[5 - coin[5]] + 1 = d[5 - 5] + 1 = 1
    i = 6 d[6] = d[6 - coin[6]] + 1 = d[6 - 2] + 1 = 3
    i = 7 d[7] = Integer.MAX_VALUE
    i = 8 d[8] = d[8 - coin[2]] + 1 = d[6 - 2] + 1 = 4
    i = 9 d[9] = Integer.MAX_VALUE
    i = 10 d[2] = d[10 - coin[5]] + 1 = d[10 - 5] + 1 = 2
    i = 11 d[11] = Integer.MAX_VALUE
    i = 12 d[12] = d[12 - coin[10]] + 1 = d[12 - 10] + 1 = 3
    */
    public int coinChange(int[] coins, int amount) {
        int [] d = new int[amount + 1];
        
        //組成0,只有0種的硬幣選擇
        d[0] = 0;
        
        //1~amount都設為無限大(表示先假設所有的數都是目前的硬幣無法達到的)
        for(int i = 1; i <= amount; i ++) {
            d[i] = Integer.MAX_VALUE;
        }
        
        //從1~amount,將所有的數過一遍
        for(int i = 1; i <= amount; i ++) {
            //每次i循環時,都要將所有的硬幣過一遍
            for(int j = 0; j < coins.length; j ++) {
                //d[i - coins[j]] == Integer.MAX_VALUE 表示該i-coins[j]個數無解
                if(i >= coins[j] && d[i - coins[j]] != Integer.MAX_VALUE) {
                    //比較d[i]無解,與d[i - coins[j]] + 1哪個小
                    d[i] = Math.min(d[i], d[i - coins[j]] + 1);
                }
            }
        }
        
        //若d[amount] == Integer.MAX_VALUE,就是沒有解
        if(d[amount] != Integer.MAX_VALUE)
            return d[amount];
        else
            return -1;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值