Skip to content
AI360Xpert
Beta
Difficulty: Medium2-D Dynamic Programming

Coin Change II

Problem in Plain English

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.

Problem Statement

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.

Constraints

  • 1 <= coins.length <= 300
  • 1 <= coins[i] <= 5000
  • All the values of coins are unique.
  • 0 <= amount <= 5000
  • The answer is guaranteed to fit into a signed 32-bit integer.

Examples

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.

Example 2

Input
amount = 3, coins = [2]
Output
0

Any number of 2-valued coins gives an even total. The odd target 3 cannot be reached, so there are zero combinations.

Example 3

Input
amount = 10, coins = [10]
Output
1

Exactly one 10-valued coin makes 10. Zero coins give 0 and two coins give 20, so there is exactly one combination.

Example 4

Input
amount = 0, coins = [2, 5]
Output
1

Choosing zero copies of every denomination is one valid empty combination. No positive coin can participate in an amount of zero.

Example 5

Input
amount = 3, coins = [2, 1]
Output
2

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.

Intuition

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.

Approaches

Recursive Enumeration

Solution Details

Reveal Recursive Enumeration: intuition, complexity, and code

Hints

Hint 1
Represent a combination by the quantity of each denomination. Which choice must be fixed to avoid counting rearrangements?
Hint 2
For a fixed coin, divide combinations into those using zero copies and those using at least one copy. Removing one copy connects the latter group to a smaller amount.
Hint 3
Use coins in the outer loop, start dp[0] at 1, and sweep amounts upward. This preserves both the denomination restriction and unlimited reuse.

Edge Cases

  • Amount zero has one empty combination even when every denomination exceeds the target.
  • An unreachable amount such as 3 with [2] returns zero; all-even denominations cannot make an odd amount.
  • Unsorted denominations such as [2,1] work without sorting. Denominations are unique by contract, so duplicated inputs need not be supported.
  • A coin larger than the target contributes no updates. One denomination dividing the target has exactly one combination, regardless of how many copies are needed.
  • Intermediate amounts can have huge counts while the final answer is zero: many even denominations with an odd target require overflow-safe table arithmetic.

Common Mistakes and Interview Tips

  • Putting amounts in the outer loop allows every denomination at each last step and counts permutations such as 1+2 and 2+1 separately.
  • Sweeping amounts downward treats each denomination as available once, as in 0/1 knapsack; this problem permits unlimited copies.
  • Initializing dp[0] to zero removes the empty-combination seed and makes all counts stay zero. In recursion, test the zero remainder before exhausted denominations.
  • Using only a remainder in a memoized recurrence loses which denominations are allowed. The recursive state needs both index and remainder.
  • Assuming the final 32-bit guarantee bounds every intermediate table cell can overflow Java int or lose JavaScript precision. Cap nonnegative counts with a proof, or use exact large integers.
  • Interview tip: explain the coin-prefix invariant and why ascending updates reuse the current coin. Quantity enumeration also avoids an amount-deep recursive stack, but remains exponential.

Key Takeaway

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.