Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. def greedyChange(myArray, value):
  2.  
  3. #assuming the array is sorted, you don't need to sort, else the array will need to be sorted into descending order so that the largest value is the first value in the array;
  4. coins = 0
  5. i = len(myArray) -1;
  6. count = 0
  7. used = []
  8. while (value > -1):
  9.  
  10. if (myArray[i] <= value):
  11.  
  12. value = value - myArray[i]
  13. coins += 1
  14. count += 1
  15.  
  16. else:
  17.  
  18. used.insert(i, count)
  19. count = 0
  20. if (value == 0):
  21. break
  22. i -= 1
  23. return used
  24.  
  25.  
  26. used = greedyChange([1, 5, 10, 25], 48)
  27. print("Coins Used: "),
  28. print(used)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement