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;
    }
}

### 功能 `int coinChange(vector<int>& coins, int amount)` 函数的主要功能是解决零钱兑换问题,即给定不同面额的硬币 `coins` 和一个总金额 `amount`,计算出凑成总金额所需的最少硬币个数。若无法凑出总金额,则返回 -1 [^1][^2][^4]。 ### 实现 #### 动态规划实现 ```cpp class Solution { public: int coinChange(vector<int>& coins, int amount) { vector<int> dp(amount + 1, amount + 1); dp[0] = 0; for (int i = 1; i <= amount; i++) { for (int j = 0; j < coins.size(); j++) { if (coins[j] <= i) dp[i] = min(dp[i], dp[i - coins[j]] + 1); } } return dp.back() > amount ? -1 : dp.back(); } }; ``` 上述代码使用动态规划的思想,创建一个长度为 `amount + 1` 的数组 `dp`,`dp[i]` 表示凑成金额 `i` 所需的最少硬币个数。初始化 `dp[0] = 0`,因为凑成金额 0 不需要任何硬币。然后通过两层循环,外层循环遍历金额从 1 到 `amount`,内层循环遍历所有硬币。对于每个硬币,如果其面值小于等于当前金额 `i`,则更新 `dp[i]` 为 `dp[i]` 和 `dp[i - coins[j]] + 1` 中的较小值 [^1][^4]。 #### 回溯 + 剪枝实现 ```cpp class Solution { public: int coinChange(vector<int>& coins, int amount) { if (amount == 0) return 0; int ret = INT_MAX; sort(coins.rbegin(), coins.rend()); coinChange(coins, amount, 0, ret, 0); return ret == INT_MAX ? -1 : ret; } void coinChange(vector<int>& coins, int amount, int count, int &ret, int index) { if (amount == 0) { ret = ret < count ? ret : count; return; } if (index == coins.size()) return; for (int k = amount / coins[index]; k >= 0 && k + count < ret; k--) { coinChange(coins, amount - k * coins[index], count + k, ret, index + 1); } } }; ``` 上述代码采用回溯 + 剪枝的方法,先将硬币从大到小排序,然后从最大面额的硬币开始,尽可能多地使用该硬币,若无法凑出金额则回溯。在回溯过程中,若当前使用的硬币数已经超过当前的最优解 `ret`,则不再向下搜索,进行剪枝 [^3]。 ### 优化方案 - **空间优化**:动态规划的实现中,由于每次状态转移只依赖于前一个状态,因此可以考虑使用滚动数组等方式进一步优化空间复杂度,但在该问题中,由于主要是一维数组,空间优化效果不明显。 - **剪枝优化**:在回溯 + 剪枝的实现中,剪枝策略是关键。可以根据实际情况进一步优化剪枝条件,减少不必要的搜索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值