LeetCode ASMR: Lemonade Change | Day 60 Coding Chill

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024
  • This is making me want to set up a lemonade stand : D

Комментарии • 1

  • @LeekCodeDev
    @LeekCodeDev  28 дней назад +2

    Source Code:
    class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
    """
    To solve this problem, I first tried to write down all the
    possible scenarios. Because I need to give my customers
    a net change of 5 dollars, 3 things can happen:
    Customer gives me $5 -> I just add my number of 5 dollar bills
    Customer gives me $10 -> I give them one of my 5 dollar bills
    if I have any. If not, I return False
    Customer gives me $20 -> I give them 1 10$ bill and 1 5$ bill,
    or give them 3 5$ bills, given that I have enough bills. If I
    don't, I return False.
    I'm going to keep my counts of 5 dollar bills and 10 dollar
    bills in a hash map. :D
    """
    bank = defaultdict(int)
    for bill in bills:
    if bill == 5:
    bank[bill] += 1
    elif bill == 10:
    if bank[5] < 1:
    return False
    else:
    bank[5] -= 1
    bank[10] += 1
    else:
    if bank[5] > 0 and bank[10] > 0:
    bank[5] -= 1
    bank[10] -= 1
    elif bank[5] >= 3:
    bank[5] -= 3
    else:
    return False
    return True