leetcode 860

今天太累了,刷道简单的拉倒吧。这题的思想很简单,就是利用两个变量来表示剩余的5块和10块的数量。此外还有一点就是对于20的,优先找10快和5快的,如果没有,再找3个5快的。因为5快的还可以找10快的。附代码:

class Solution:
    def lemonadeChange(self, bills):
        """
        :type bills: List[int]
        :rtype: bool
        """
        five = 0
        ten = 0
        for bill in bills:
            if bill == 5:
                five += 1
            elif bill == 10:
                if five == 0:
                    return False
                else:
                    five -= 1
                    ten += 1
            else:
                if ten > 0 and five > 0 :
                    ten -=1
                    five -=1
                elif five > 2:
                    five -=3
                else:
                    return False
        return True
                       

 

你可能感兴趣的:(leetcode)