程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[leetcode question brushing Python] 860 Lemonade change

編輯:Python

1 subject

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 .

2 analysis

(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

3 Python Realization

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved