LeetCode Coin Change Series

classic dp problem
322
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.

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

this is a classic knapsack problem.
dp[amount+1][coins.length+1]
dp[i][j] means that we only have amount of i needs to get change, and we only have j kind of coins to change
i tried to write that, but i failed,

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = 0;//we coin is 0
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                if (j - coins[i-1] > 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount];
    }
}

and then i know what i did wrong

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = Integer.MAX_VALUE;//the amount of that
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                if (j - coins[i-1] > 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount]; 
    }
}

but it’s overflowed still.
because of the dp[i-1][j - coins[i-1]] + 1 part might makes it overflowed.
so we need to assign dp[0][i] = amount+1 //don’t use it too precise, you will just make yourself look bad.

class Solution {
    public int coinChange(int[] coins, int amount) {
        int m = coins.length;
        int[][] dp = new int[coins.length+1][amount+1];
        dp[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            dp[i][0] = 0; //when amount is 0
        }
        for (int i = 1; i<=amount; i++) {
            dp[0][i] = amount + 1;//unreachable 
        }
        for (int j = 1; j <= amount; j++) {
            for (int i = 1; i<= m; i++) { //because we can choose as many coins as we want. that's why we put coins in the inner loop
                
                if (j - coins[i-1] >= 0) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i][j - coins[i-1]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];//didn't choose this becasue of not enought space
                }
            }
        }
        return dp[m][amount] > amount? -1: dp[m][amount]; 
    }
}

518
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
//but in this problem, order is not matter
//I found out that this problem is a more general version of k sum, k sum means you have to do it using k number. but this problem can use as many as you can.
//permuatation is easy than combination, because combination needs to eliminate all the duplicates results in permutation.
//we can see in the code, it is very simililar to LC377, except, now amount is the inner loop? why does the amount be the inner loop makes the solution didn’t count the duplicates.

we use 1D array to solve this:
dp[i] reepresents for the number of combinations if the amount the money we need to change is i

class Solution {
    public int change(int amount, int[] coins) {
        if(amount == 0) return 1;
        if(coins == null || coins.length == 0) return 0;
        
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        
        for(int i = 0; i<coins.length; i++) {
            for (int j = 1; j<=amount; j++) {
                if(j-coins[i] >= 0) {
                    dp[j] += dp[j - coins[i]];  //because we can choose one single kind of coin unlimited time, that's why we need to make that outer loop
                }
            }
        }
        // for(int j = 1; j<=amount; j++) {
        //     for (int i = 0; i<coins.length; i++) {
        //         if(j-coins[i] >= 0) {
        //             dp[j] += dp[j - coins[i]]; 
        //         }
        //     }
        // }
        return dp[amount];
    }
}

now, we compare this code to LC377 combination sum IV

//now, the problem is: can we using 2-D array to solve this problem?
//and the answer is yes!
//the code is as follows: and the inner loop and outer loop can reversed.
// for(int i=1;i<coins.length;i++)
//   {
//     for(int j=1;j<=amount;j++)
//     {
//       if(j>=coins[i])
//         dp[i][j]=dp[i-1][j]+dp[i][j-coins[i]];
//       else
//          dp[i][j]=dp[i-1][j];
//     }
//   }
class Solution {
    public int combinationSum4(int[] nums, int target) {
        
        if(target == 0) return 1;
        if(nums == null || nums.length == 0) return 0;
        
        int[] dp = new int[target+1];
        for(int i = 1; i<=target; i++) {
            dp[i] = 0;
        }
        dp[0] = 1;
        
        //so the general idea of follow for loops, is: for each target, we will try all the coins, if we can use this coin, then fine. after we used this coin, nothing is change in nums[] becasue each one of them has unlimited number.
        for(int i = 1; i<=target; i++) {
            for(int j = 0; j<nums.length; j++) { //try every kind of coin
                if(i-nums[j] >= 0) { //if current coin value <= current target, means we can cover it. so now dp[i] is its origianl number of ways plus if we choose nums[j]
                    dp[i] += dp[i-nums[j]];
                }
            }
        }
        return dp[target];
    }
}
上一篇:八.枚举与模式匹配


下一篇:硬币找零问题-贪婪法与动态规划