I am trying to implement greedy approach in coin change problem, but need to reduce the time complexity because the compiler won't accept my code, and since I am unable to verify I don't even know if my code is actually correct or not. The function should return the total number of notes needed to make the change. If change cannot be obtained for the given amount, then return -1. Return 1 if the amount is equal to one of the currencies available in the denomination list.
def make_change(denomination_list, amount):
denomination_list.sort()
n = len(denomination_list)
i = n - 1
ans = 0
x = 0
while i>= 0 :
while denomination_list[i]<= amount:
ans = +1
amount -= denomination_list[i]
i -= 1
if amount == 0:
x = 1
elif amount<0:
x = -1
return x
amount= 20
denomination_list = [1,15,10]
print(make_change(denomination_list, amount))
-=instead of=-for a start. - One Lyner1000000 // 15is 66666 and1000000 % 15is 10. - user3386109