zoukankan      html  css  js  c++  java
  • [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.

    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.

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

    给一些可用的硬币面值,又给了一个找零钱数,问最小能用几个硬币来组成。跟CareerCup上的9.8 Represent N Cents 美分的组成有些类似,那道题给全了所有的美分,25,10,5,1,然后给一个钱数,问所有能够找零的方法。

    解法:动态规划DP。建立一个一维数组dp,dp[i]表示钱数为i时需要的最少的硬币数,dp[i] = min(dp[i], dp[i - coins[j]] + 1)

    Java:

    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:

    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: wo

    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 题目汇总

  • 相关阅读:
    关于RAM的空间使用超过限度的时候报错
    (转载)关于stm32编译后的代码空间和ram占用
    PCB文件过大的解决方法
    AD15的破解
    AD2017破解步骤
    STM32下载报错invalid rom table
    (转载)关于FLASH寿命的读写方法
    步进电机的单双极驱动
    74系列芯片中的LVC,LS,HC等的含义
    DDR3 multi-controller on ML605
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8674185.html
Copyright © 2011-2022 走看看