AlgoFrames

Coin change II

Standard · Write code · about 12 min

Return the number of different coin sets that add up to amount, using unlimited coins of each value in coins. Sets that differ only in order count once. Return 0 if no set works.

Example 1

Input:

[5,[1,2,5]]

Output:

4

5, 2+2+1, 2+1+1+1 and 1+1+1+1+1.

Example 2

Input:

[3,[2]]

Output:

0

No set of 2s makes 3.

Constraints

  • 0 ≤ amount ≤ 1000
  • 1 ≤ coins.length ≤ 1000
  • 1 ≤ coins[i] ≤ 1000, all different
  • The answer fits in a 32-bit signed integer

Learn the idea first: Coin change: minimum coins and number of ways, a visual lesson.

More Coin change: minimum coins and number of ways problems