Example 1
- Input
- amount = 5, coins = [1, 2, 5]
- Output
- 4
There are four quantity choices: five 1s; three 1s and one 2; one 1 and two 2s; or one 5. Rearranging the same coins does not create another combination.
Given coin denominations with unlimited supply and a target amount, count how many different combinations of coins add up to exactly that amount, where order does not matter.
You are given an integer array coins of different denominations and an integer amount. Return the number of combinations that make up exactly that amount, using any number of each coin including zero.
Combinations ignore order: 2 + 2 + 1 and 1 + 2 + 2 are the same combination and must be counted once. If nothing adds up to amount, return 0.
There is exactly one way to make amount 0: use no coins at all.
1 <= coins.length <= 3001 <= coins[i] <= 5000coins are unique.0 <= amount <= 5000There are four quantity choices: five 1s; three 1s and one 2; one 1 and two 2s; or one 5. Rearranging the same coins does not create another combination.
Any number of 2-valued coins gives an even total. The odd target 3 cannot be reached, so there are zero combinations.
Exactly one 10-valued coin makes 10. Zero coins give 0 and two coins give 20, so there is exactly one combination.
Choosing zero copies of every denomination is one valid empty combination. No positive coin can participate in an amount of zero.
The combinations are three 1s and one 2 with one 1. The input need not be sorted: 2+1 and 1+2 still describe the same quantity choices.
A combination is determined by how many copies of each denomination it uses, not by their order. For amount 5 with [1, 2, 5], the quantities are (5,0,0), (3,1,0), (1,2,0), and (0,0,1). Fixing denominations in the input order gives each quantity vector one path. Dynamic programming shares the work for smaller amounts while retaining that coin-order rule; it counts combinations rather than arrangements.
Reveal Recursive Enumeration: intuition, complexity, and code
For unlimited-use combination counting, fix an item order and process one denomination completely before the next. The coin-prefix invariant removes reordered duplicates, while ascending capacity updates allow repeated use. Changing either loop order or traversal direction changes what the table counts.