CodeArchon

medium2-D Dynamic ProgrammingAmazon

Combo Coin Counter

An accountant counts the distinct combinations of unlimited-stock coin denominations that total an exact amount.

Given coins of different denominations and amount, return the number of combinations that make that amount.

Choose approach

1
?
?
?
?
?
1
?
?
?
?
?
1
?
?
?
?
?
1
?
?
?
?
?
1/5
Narrationstep 1 of 5

Seed first column with 1 (one way to make 0).

1def change(amount, coins):
2 dp = [0] * (amount + 1); dp[0] = 1
3 for c in coins:
4 for a in range(c, amount + 1):
5 dp[a] += dp[a - c]
6 return dp[amount]
Variables
amount=5
coins=1,2,5

How to think about it

  1. Reversed loop order would count permutations, not combos.
  2. Equivalent 2-D form dp[i][a] shown here.

Edge cases to catch in interview

  • amount=0 -> 1.
  • No coin divides anything -> 0.

Examples

Example 10
{"amount":3,"coins":[2]}

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed