Dynamic Programming: Coin Change

The fewest coins that make 6 with coins 1, 3 and 4. Greedy says 4 + 1 + 1; dynamic programming finds 3 + 3.

Python · intermediate · 10 min · 67 steps · free

What you'll learn

coin_change.py

def min_coins(coins, amount):
    best = [0] + [float('inf')] * amount
    for total in range(1, amount + 1):
        for coin in coins:
            if coin <= total:
                best[total] = min(best[total], best[total - coin] + 1)
    return best[amount]

print(min_coins([1, 3, 4], 6))

Step by step

  1. Python defines the function min_coins(). Its body does not run yet: it runs each time the function is called.
  2. Python calls min_coins(), with coins = [1, 3, 4] and amount = 6. It gets its own names.
  3. best[x] will hold the fewest coins for amount x. best[0] = 0; the rest start as infinity (not reachable yet).
  4. total is created with the value 1.
  5. coin is created with the value 1.
  6. coin <= total is true, so the block under it runs.
  7. Amount 1: one coin of 1.
  8. coin changes from 1 to 3.
  9. coin <= total is false, so the block under it is skipped.
  10. coin changes from 3 to 4.
  11. coin <= total is false, so the block under it is skipped.
  12. Python moves to the next item of the loop: for coin in coins.
  13. total changes from 1 to 2.
  14. coin changes from 4 to 1.
  15. coin <= total is true, so the block under it runs.
  16. In the list that best points to, index 2 changes from inf to 2.
  17. coin changes from 1 to 3.
  18. coin <= total is false, so the block under it is skipped.
  19. coin changes from 3 to 4.
  20. coin <= total is false, so the block under it is skipped.
  21. Python moves to the next item of the loop: for coin in coins.
  22. total changes from 2 to 3.
  23. coin changes from 4 to 1.
  24. coin <= total is true, so the block under it runs.
  25. In the list that best points to, index 3 changes from inf to 3.
  26. coin changes from 1 to 3.
  27. coin <= total is true, so the block under it runs.
  28. Amount 3 with the 3 coin: best[0] + 1 = 1 coin, better than 1 + 1 + 1.
  29. coin changes from 3 to 4.
  30. coin <= total is false, so the block under it is skipped.
  31. Python moves to the next item of the loop: for coin in coins.
  32. total changes from 3 to 4.
  33. coin changes from 4 to 1.
  34. coin <= total is true, so the block under it runs.
  35. In the list that best points to, index 4 changes from inf to 2.
  36. coin changes from 1 to 3.
  37. coin <= total is true, so the block under it runs.
  38. Line runs: best[total] = min(best[total], best[total - coin] + 1).
  39. coin changes from 3 to 4.
  40. coin <= total is true, so the block under it runs.
  41. In the list that best points to, index 4 changes from 2 to 1.
  42. Python moves to the next item of the loop: for coin in coins.
  43. total changes from 4 to 5.
  44. coin changes from 4 to 1.
  45. coin <= total is true, so the block under it runs.
  46. In the list that best points to, index 5 changes from inf to 2.
  47. coin changes from 1 to 3.
  48. coin <= total is true, so the block under it runs.
  49. Line runs: best[total] = min(best[total], best[total - coin] + 1).
  50. coin changes from 3 to 4.
  51. coin <= total is true, so the block under it runs.
  52. Line runs: best[total] = min(best[total], best[total - coin] + 1).
  53. Python moves to the next item of the loop: for coin in coins.
  54. total changes from 5 to 6.
  55. coin changes from 4 to 1.
  56. coin <= total is true, so the block under it runs.
  57. Amount 6 with a 1 coin: best[5] + 1 = 3 coins (4 + 1 + 1).
  58. coin changes from 1 to 3.
  59. coin <= total is true, so the block under it runs.
  60. With a 3 coin: best[3] + 1 = 2 coins (3 + 3). Better!
  61. coin changes from 3 to 4.
  62. coin <= total is true, so the block under it runs.
  63. Line runs: best[total] = min(best[total], best[total - coin] + 1).
  64. Python moves to the next item of the loop: for coin in coins.
  65. Python moves to the next item of the loop: for total in range(1, amount + 1).
  66. The answer for 6: 2 coins.
  67. min_coins() has finished, so its names are gone. It prints 2.

Common mistakes

Best practices

Key takeaways

Check yourself

Cost for amount A and C coins?

Answer: O(A × C)

best[0] is?

Answer: 0. No coins make 0.

More in Backtracking and Dynamic Programming