iq.lab
Python starts when a code cell comes near or you run one
mediumDynamic programming, 1D target 25 min

Count coin combinations

You get a whole number amount and a list coins of different coin values. You have as many coins of each value as you want. Return how many different combinations of coins add up to exactly amount.

In a combination only the number of coins of each value matters, not their order: 1 + 2 and 2 + 1 are the same combination. When amount is 0 the answer is 1, the combination with no coins. When no combination works, the answer is 0.

Example 1
Inputamount = 4, coins = [1, 2, 3]Output4

1 + 1 + 1 + 1, 1 + 1 + 2, 2 + 2 and 1 + 3. Counting orders as different would give 7, because 1 + 1 + 2 has three orders and 1 + 3 has two.

Example 2
Inputamount = 7, coins = [2, 4]Output0

Every combination of 2s and 4s adds up to an even number, and 7 is odd.

Example 3
Inputamount = 0, coins = [5]Output1

Using no coins makes 0.

Constraints
  • 0 ≤ amount ≤ 5,000

  • 1 ≤ len(coins) ≤ 300

  • 1 ≤ coins[i] ≤ 5,000

  • The values in coins are all different.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.