Saturday, January 12, 2019

322. Coin Change 518. Coin Change 2

322Coin 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:
Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Note:
You may assume that you have an infinite number of each kind of coin.
---------------------------
DP, 遍历1 - amount每一个数,找出该数所需要的最小个数
dp[i] 存的是在amount i下最小所需要的个数
class Solution {
    public int coinChange(int[] coins, int amount) {
        
        int[] dp = new int[amount + 1]; 
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        
        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.length; j++) {
                if (coins[j] <= i) {
                    
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                }
            }        
        }
        
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }
}

Solution #2, memoization
class Solution {
    public int coinChange(int[] coins, int amount) {
        
        int[] mem = new int[amount + 1]; 
        mem[0] = 0;
        
        return rec(coins, amount, mem);
    }
    
    private int rec(int[] coins, int amount, int[] mem) {
        if (amount < 0) return -1;
        if (amount == 0) return 0;
        if (mem[amount] != 0) return mem[amount];
        
        int min = Integer.MAX_VALUE;
        for (int c : coins) {
            int count = rec(coins, amount - c, mem);
            if (count != -1) min = Math.min(min, count);
        }
        
        if (min == Integer.MAX_VALUE) mem[amount] = -1;
        else  mem[amount] = min + 1;
        
        return mem[amount];
    }
}


518Coin Change 2
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.

    Example 1:
    Input: amount = 5, coins = [1, 2, 5]
    Output: 4
    Explanation: there are four ways to make up the amount:
    5=5
    5=2+2+1
    5=2+1+1+1
    5=1+1+1+1+1
    
    Example 2:
    Input: amount = 3, coins = [2]
    Output: 0
    Explanation: the amount of 3 cannot be made up just with coins of 2.
    
    Example 3:
    Input: amount = 10, coins = [10] 
    Output: 1
    

    Note:
    You can assume that
    • 0 <= amount <= 5000
    • 1 <= coin <= 5000
    • the number of coins is less than 500
    • the answer is guaranteed to fit into signed 32-bit integer
    ---------------
    dp[i] 是在amount i时的number of combination,则最后结果返回dp[amount]
    通项公式
    dp[i] = dp[i - coins[0]] + dp[i - coins[1]] + ... + dp[i - coins[coins.length - 1]] if i - coins[0] >= 0

    class Solution {
        public int change(int amount, int[] coins) {
            int[] dp = new int[amount + 1];
            dp[0] = 1;
            
            for (int i = 0; i < coins.length; i++) {
                for (int j = 1; j < amount + 1; j++) {
                    if (j >= coins[i]) {
                        dp[j] += dp[j - coins[i]];
                    }
                }
            }
            
            return dp[amount];
        }
    
    }
    

    No comments:

    Post a Comment