129. Counting Ways to Make an Amount
Medium
A shop accepts coins of several denominations, and there is an unlimited supply of each one. Given the available denominations in coins and a value amount, count the combinations of coins that sum to exactly that value. The order of the coins does not matter: using a coin worth 1 followed by one worth 2 represents the same combination as using the coin worth 2 followed by the one worth 1.
Return the number of combinations modulo 1,000,000,007. If amount is zero, count exactly one combination: choose no coins.
Example 1
Input: coins = [1, 2, 5], amount = 5
Output: 4
Explanation: The combinations are five coins worth 1, three coins worth 1 and one worth 2, one worth 1 and two worth 2, or one coin worth 5.
Example 2
Input: coins = [2], amount = 3
Output: 0
Explanation: It is not possible to make 3 using only coins worth 2.
Example 3
Input: coins = [], amount = 0
Output: 1
Explanation: There is exactly one way to make zero: use no coins.
Constraints
- 0 <= coins.length <= 2000
- 1 <= coins[i] <= 2000
- The values in `coins` are distinct.
- 0 <= amount <= 2000
Dynamic programming
Saved in this browser
Caso 1
coins =
[1,2,5]
amount =
5
Expected =
4
Caso 2
coins =
[2]
amount =
3
Expected =
0
Caso 3
coins =
[]
amount =
0
Expected =
1
Run checks these 3 cases. Submit runs all 12, including hidden ones.