python C++ Greedy速解找零問題Leetcode 860 Lemonade Change
b=20時貪婪演算法保持檢查順序,先找十塊,不要改變檢查順序,否則不起作用
[python解請進]
When b=20, the greedy algorithm maintains the checking order. Give back $10 first, and do not change the checking order, otherwise it will not work.
class Solution: def lemonadeChange(self, bills: List[int]) -> bool: cnt5, cnt10=0, 0 for b in bills: if b==5: cnt5+=1 elif b==10: if cnt5>0: cnt5-=1 cnt10+=1 else: return False else: if cnt10>0 and cnt5>0: cnt10-=1 cnt5-=1 elif cnt5>2: cnt5-=3 else: return False return True
沒有留言:
張貼留言