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

  • 相关阅读:
    程序员学习方法差在哪里
    解析域名
    tomcat下的公共jar包配置
    Ubuntu 16.04 修改状态栏位置
    sqlite3 C语言 API 函数
    vim配置文件
    关于 ioctl 函数
    字符设备基础了解
    Ubuntu14.04搭建Boa服务
    gcc 交叉工具链中工具使用(arm-linux-xxx)
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8674185.html
Copyright © 2020-2023  润新知