philRG

Greedy algo (money change)

Sep 2nd, 2021 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.29 KB | None | 0 0
  1. coins = {5: 0, 2: 0, 1: 0}
  2. money = 28
  3.  
  4. sum = 0
  5. while sum != money:
  6.     matching_coins = [c for c in coins if c + sum <= money]
  7.     if matching_coins:
  8.         coin = max(matching_coins)
  9.         coins[coin] += 1
  10.         sum += coin
  11.     else:
  12.         print("No solution")
  13. print(coins)
  14.  
  15.  
Add Comment
Please, Sign In to add comment