[LeetCode] 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.html

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

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

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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.this

給一些可用的硬幣面值,又給了一個找零錢數,問最小能用幾個硬幣來組成。跟CareerCup上的9.8 Represent N Cents 美分的組成有些相似,那道題給全了全部的美分,25,10,5,1,而後給一個錢數,問全部可以找零的方法。url

解法:動態規劃DP。創建一個一維數組dp,dp[i]表示錢數爲i時須要的最少的硬幣數,dp[i] = min(dp[i], dp[i - coins[j]] + 1)spa

Java:.net

class Solution {
    public int coinChange(int[] coins, int amount) {
        if(amount==0) return 0;

        int[] dp = new int [amount+1];
        dp[0]=0; // do not need any coin to get 0 amount
        for(int i=1;i<=amount; i++)
            dp[i]= Integer.MAX_VALUE;

        for(int i=0; i<=amount; i++){
            for(int coin: coins){
                if(i+coin <=amount){
                    if(dp[i]==Integer.MAX_VALUE){
                        dp[i+coin] = dp[i+coin];
                    }else{
                        dp[i+coin] = Math.min(dp[i+coin], dp[i]+1);
                    }
                }
            }
        }

        if(dp[amount] >= Integer.MAX_VALUE)
            return -1;

        return dp[amount];
    }
}  

Python:code

class Solution(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        INF = 0x7fffffff  # Using float("inf") would be slower.
        amounts = [INF] * (amount + 1)
        amounts[0] = 0
        for i in xrange(amount + 1):
            if amounts[i] != INF:
                for coin in coins:
                    if i + coin <= amount:
                        amounts[i + coin] = min(amounts[i + coin], amounts[i] + 1)
        return amounts[amount] if amounts[amount] != INF else -1

Python: wohtm

class Solution(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        MAX = float('inf')
        dp = [MAX] * (amount + 1)
        dp[0] = 0
        for i in xrange(amount):
            if dp[i] != MAX:
                for coin in coins:
                    if i + coin <= amount:
                        dp[i+coin] = min(dp[i+coin], dp[i] + 1)            

        return dp[-1] if dp[-1] != MAX else -1  

C++:

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> amounts(amount + 1, numeric_limits<int>::max());
        amounts[0] = 0;
        for (int i = 0; i <= amount; ++i) {
            if (amounts[i] != numeric_limits<int>::max()) {
                for (const auto& coin : coins) {
                    if (coin <= numeric_limits<int>::max() - i && i + coin <= amount) {
                        amounts[i + coin] = min(amounts[i + coin], amounts[i] + 1);
                    }
                }
            }
        }
        return amounts[amount] == numeric_limits<int>::max() ? -1 : amounts[amount];
    }
};

 

相似題目:

[CareerCup] 9.8 Represent N Cents

[LeetCode] 377. Combination Sum IV 組合之和 IV

 

All LeetCode Questions List 題目彙總

相關文章
相關標籤/搜索