LeetCode(力扣)860. 柠檬水找零Python

LeetCode860. 柠檬水找零

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/lemonade-change/
LeetCode(力扣)860. 柠檬水找零Python_第1张图片

代码

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

你可能感兴趣的:(leetcode,python,算法,职场和发展)