【leetCode-DP-完全揹包】518. 零錢兌換 II (三種解法、兩種優化)

給定不同面額的硬幣和一個總金額。寫出函數來計算可以湊成總金額的硬幣組合數。假設每一種面額的硬幣有無限個。 


 

示例 1:

輸入: amount = 5, coins = [1, 2, 5]
輸出: 4
解釋: 有四種方式可以湊成總金額:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
示例 2:

輸入: amount = 3, coins = [2]
輸出: 0
解釋: 只用面額2的硬幣不能湊成總金額3。
示例 3:

輸入: amount = 10, coins = [10] 
輸出: 1

注意:

你可以假設:

0 <= amount (總金額) <= 5000
1 <= coin (硬幣面額) <= 5000
硬幣種類不超過 500 種
結果符合 32 位符號整數

暴力搜索

根據題目可以使用暴力搜索解決,但是由於amount和coin的種類的限定2^5000一定是超時的,提交的一發果不其然。code如下:

public static int change(int amount, int[] coins) {
        int[] path = new int[amount];
        Arrays.sort(coins);
        return dfs(coins,amount,0,path);
    }

    private static int dfs(int[] coins, int amount,int cur,int[] path) {
        if(amount == 0) {
            return 1;
        }else if(amount < 0) {
            return 0;
        }
        int results = 0;
        for(int t = 0; t < coins.length; t++) {
            if( amount >= coins[t] ) {
                if(0==cur || path[cur-1] <= coins[t]) {
                    path[cur] = coins[t];
                    int r = dfs(coins, amount - coins[t], cur + 1, path);
                    results += r;
                }
            } else {
                break;
            }
        }
        return results;
    }

記憶化搜索優化搜索

定義arrs[S]代表表示面值爲S時的組合數. 在搜索的時候如何arrs[]數組中的值不是-1的話直接只用當前的數

現在考慮由d[S-coins[i]]能否算出arrs[S]。

例如:考慮面值S=1,2,3,4,5,6時,coins=[1,2,5]的組合情形

arrs[1]=1,arrs[2]=2

現在,面值S=3,考慮arrs[3],

    選中coins[0] = 1 , S - coins[0] = 2 。而arrs[2] = 2 , d[3] = 2

    選中coins[1] = 2 ,S - coins[1] = 1 。而arrs[1] = 1,d[3] =2+ 1 = 3

顯然arrs[3] != 3 是2

 

這說明d[S]的定義無法排除重複組合。需要擴充定義。

設d[S][j]表示面值爲S,組合的第一個硬幣是coins[j]時的組合種數。還是要求組合中,硬幣的面值遞增。

d[1][0]=1

d[2][0]=1 , d[2][1]=1

那麼d[3][0],剩餘面值3-1=2。面值2時,選擇第一個硬幣是coins[0]=1。d[2][0]=1。 選擇第一個硬幣是coins[1]=2。d[2][1]=1。 d[3][0]=d[2][0]+d[2][1]=2。

d[3][1], 剩餘面值3-2=1。面值1小於面值2,這種組合被排除。 d[3][1] =0。

面值S=3的總組合數=d[3][0]+d[3][1]=2+0=2.

code:

    public int change(int amount, int[] coins) {
        int[] path = new int[amount+1];
        //arrs[s][i] 表示截至第i個數組成s一共有多少種
        int[][] arrs = new int[amount+1][coins.length+1];
        for(int t = 0;t <= amount;t++)
            Arrays.fill(arrs[t],-1);
        Arrays.sort(coins);
        int rs = dfsOPT(coins,amount,0,path,arrs);
    
        return rs;
    }

    private  int dfsOPT(int[] coins, int amount,int cur,int[] path,int[][] arrs) {
        if (amount == 0) {
            return 1;
        } else if(amount < 0) {
            return 0;
        }
        int results = 0;
        for(int t = 0; t < coins.length; t++) {
            if( amount >= coins[t] ) {
                if(0==cur || path[cur-1] <= coins[t]) {
                    if(arrs[amount][t] != -1) {
                        results += arrs[amount][t];
                    } else {
                        arrs[amount][t] = 0;
                        path[cur] = coins[t];
                        int r = dfsOPT(coins, amount - coins[t], cur + 1, path,arrs);
                        arrs[amount][t] = r;
                        results += r;
                    }
                }
            } else {
                break;
            }
        }
        return results;
    }

 

完全揹包

經典完全揹包完全揹包求解 答案總數,直接上代碼:

    public static int change(int amount, int[] coins) {
        int len = coins.length;
        //dp[i]表示截至數組中第i個數有多少種方式成amount的錢數
        int[] dp = new int[amount+1];
        dp[0] = 1;
        for(int i = 0;i < len;i++){
            for(int j = coins[i];j <= amount;j++){
                dp[j] += dp[j-coins[i]];
            }
        }
        return dp[amount];
    }

總結

       記憶化搜索可能需要保存一個巨大的記錄矩陣,但是動態規劃有時可以採用滾動數組來減掉一維或更多,空間有優勢。(你的搜索用獨特的記錄方式除外)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章