其他分享
首页 > 其他分享> > leetcode(24)贪心系列题目

leetcode(24)贪心系列题目

作者:互联网

860. 柠檬水找零

优先消耗10美元,因为5美元的找零用处更大,能多留着就多留着

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        five, ten = 0, 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

标签:24,return,ten,bill,找零,else,five,leetcode,贪心
来源: https://www.cnblogs.com/ttyangY77/p/16340003.html