LeetCode刷题:322. Coin Change
原题链接:https://leetcode.com/problems/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:
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.
题目大意:
你会得到不同面值的硬币和总金额。写一个函数来计算你所需要的最少的硬币数量面值之和满足总金额。
如果这些硬币的任何组合都不能满足这个总金额,则返回-1。
算法分析:
这个问题如果使用贪心算法(Greedy)解题的话,在一些