Skip to content

Instantly share code, notes, and snippets.

@SharanSMenon
Created April 28, 2018 23:01
Show Gist options
  • Save SharanSMenon/6e4efce0692e4662bf39e5aa90fea982 to your computer and use it in GitHub Desktop.
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
# 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