Created
April 28, 2018 23:01
-
-
Save SharanSMenon/6e4efce0692e4662bf39e5aa90fea982 to your computer and use it in GitHub Desktop.
A greedy algorithm for calculating
change used by cashiers all over the
world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Uses python3 | |
import sys | |
from functools import reduce | |
def get_change(m): | |
""" | |
:type m: int | |
:param m: | |
:return: int | |
""" | |
R = [] | |
denominations = [1, 5, 10] | |
while True: | |
if m == 0: | |
return len(R) | |
for i in denominations: | |
if i < m: | |
mx = i | |
# print(l) | |
# print(mx) | |
m -= mx | |
R.append(mx) | |
if __name__ == '__main__': | |
# m = int(sys.stdin.read()) | |
m = 95774 | |
print(get_change(m)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment