2241. Design an ATM Machine

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2241

class ATM:
    def __init__(self):
        self.cnt = [0] * 5
        self.d = [20, 50, 100, 200, 500]

    def deposit(self, banknotesCount: List[int]) -> None:
        for i, v in enumerate(banknotesCount):
            self.cnt[i] += v

    def withdraw(self, amount: int) -> List[int]:
        res = [0] * 5
        for i in range(4, -1, -1):
            res[i] = min(amount // self.d[i], self.cnt[i])
            amount -= res[i] * self.d[i]
        if amount > 0:
            return [-1]
        for i, v in enumerate(res):
            self.cnt[i] -= v
        return res


# Your ATM object will be instantiated and called as such:
# obj = ATM()
# obj.deposit(banknotesCount)
# param_2 = obj.withdraw(amount)