程序媛詹妮弗
终身学习

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

 

题意:

给你一些不同币值的硬币,问你最少需要多少个硬币才能组成amount,假设每种硬币有无穷多个

要amount为11,我们当然可以这样:

但要求amount为11,且硬币数量最少,那么答案应该是:

 --> 返回最少硬币数量为3 

 

 

Solution1 

 

posted on 2019-05-16 03:44  程序媛詹妮弗  阅读(180)  评论(0编辑  收藏  举报