322. Coin Change(零钱兑换)
题目大意
You are given an integer array coins
representing coins of different denominations and an integer amount
representing a total amount of money.
Return 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.
You may assume that you have an infinite number of each kind of coin.
中文释义
给定一个表示不同面额硬币的整数数组 coins
和一个表示总金额的整数 amount
。
返回组成该金额所需的最少硬币数量。如果无法用这些硬币的任何组合来组成该金额,则返回 -1。
你可以假设你拥有每种硬币的无限数量。
示例
- 示例 1:
- 输入:
coins = [1,2,5]
,amount = 11
- 输出:
3
- 解释:11 = 5 + 5 + 1
- 输入:
- 示例 2:
- 输入:
coins = [2]
,amount = 3
- 输出:
-1
- 输入:
- 示例 3:
- 输入:
coins = [1]
,amount = 0
- 输出:
0
- 输入:
限制条件
1 <= coins.