#111 · Dynamic programming · Intermediate · 85 XP · about 15 min
You have as many coins as you like of each value in coins. Write count_combinations(coins, amount) that returns how many different ways there are to pay exactly amount.
Only which coins you use matters, not their order: 1 + 2 and 2 + 1 are the same way. Paying 0 has exactly one way, which uses no coins.
count_combinations([1, 2, 3], 4) # returns 4 count_combinations([2, 5, 10], 20) # returns 6 count_combinations([3], 7) # returns 0
The four ways to pay 4 are 1 + 1 + 1 + 1, 1 + 1 + 2, 2 + 2 and 1 + 3.
1 <= len(coins) <= 20, and the values are all different1 <= coins[i] <= 50000 <= amount <= 5000Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.