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

  • 相关阅读:
    R语言数据读入函数read.table
    R语言最小浮点数
    R语言randomForest包实现随机森林——iris数据集和kyphosis数据集
    R语言 rwordseg包的下载
    R语言AMORE包实现BP神经网络——German数据集
    R语言清除内存,无法分配大小为324.5 Mb的矢量
    R语言多重共现性的检测
    Machine Learning for hackers读书笔记(六)正则化:文本回归
    Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量
    完整的Django入门指南学习笔记4
  • 原文地址:https://www.cnblogs.com/lightwindy/p/8674185.html
Copyright © 2020-2023  润新知