On the lemonade stand , The price of each lemonade is 5 dollar . Customers line up to buy your products ,( By bill bills The order of payment ) Buy one cup at a time .
Each customer only buys a glass of lemonade , Then pay you 5 dollar 、10 US dollars or 20 dollar .
You have to give every customer the right change , That is to say, the net transaction is that every customer pays you 5 dollar .
Be careful , You didn't have any change at first .
Give you an array of integers bills , among bills[i] It's No i A customer paid the bill . If you can give every customer the right change , return true , Otherwise return to false .
(1) First calculate the change change
(2) Change first 10 The block , Until there is no 10 Yuan , Look again 5 The block
(3) Finally, the money received , Count
def lemonadeChange(self, bills: List[int]) -> bool:
twity,ten,five = 0,0,0
for i in range(len(bills)):
change = bills[i]-5
while(change>=10 and ten>0) :
change -=10
ten -=1
while(change>=5 and five>0) :
change -=5
five -=1
if change>0:return False
if bills[i]==20: twity+=1
elif bills[i]==10:ten+=1
else: five +=1
return True