Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Design an ATM Machine

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2241" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

There is an ATM machine that stores banknotes of 5 denominations: 20, 50, 100, 200, and 500 dollars. Initially the ATM is empty. The user can use the machine to deposit or withdraw any amount of money. When withdrawing, the machine prioritizes using banknotes of larger values. For example, if you want to withdraw $300 and there are 2 $50 banknotes, 1 $100 banknote, and 1 $200 banknote, then the machine will use the $100 and $200 banknotes. However, if you try to withdraw $600 and there are 3 $200 banknotes and 1 $500 banknote, then the withdraw request will be rejected because the machine will first try to use the $500 banknote and then be unable to use banknotes to complete the remaining $100. Note that the machine is not allowed to use the $200 banknotes instead of the $500 banknote. Implement the ATM class: ATM() Initializes the ATM object. void deposit(int[] banknotesCount) Deposits new banknotes in the order $20, $50, $100, $200, and $500. int[] withdraw(int amount) Returns an array of length 5 of the number of banknotes that will be handed to the user in the order $20, $50, $100, $200, and $500, and update the number of banknotes in the ATM after withdrawing. Returns [-1] if it is not possible (do not withdraw any banknotes in this case). Example 1: Input ["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"] [[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]] Output [null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]] Explanation ATM atm = new ATM(); atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes, // and 1 $500 banknote. atm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote // and 1 $500 banknote. The banknotes left over in the // machine are [0,0,0,2,0]. atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote. // The banknotes in the machine are now [0,1,0,3,1]. atm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote // and then be unable to complete the remaining $100, // so the withdraw request will be rejected. // Since the request is rejected, the number of banknotes // in the machine is not modified. atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote // and 1 $500 banknote. Constraints: banknotesCount.length == 5 0 <= banknotesCount[i] <= 109 1 <= amount <= 109 At most 5000 calls in total will be made to withdraw and deposit. At least one call will be made to each function withdraw and deposit. Sum of banknotesCount[i] in all deposits doesn't exceed 109

Explanation

  • Greedy Withdrawal: The withdraw function attempts to fulfill the withdrawal amount using a greedy approach, prioritizing larger denominations. It iterates through the denominations from largest to smallest, using as many banknotes as possible of each denomination without exceeding the available quantity or the remaining amount.
    • Banknote Management: The ATM class maintains an array banknotes representing the quantity of each denomination. The deposit function updates these quantities, and the withdraw function subtracts the used banknotes, only if the withdrawal is successful.
    • Withdrawal Failure Handling: If at any point during the withdrawal process, the greedy approach fails to completely fulfill the required amount, the withdraw function returns [-1] to indicate that the withdrawal is not possible, and no changes are made to the banknotes array.
  • Runtime Complexity: O(1) for both deposit and withdraw, since the number of denominations is fixed at 5. Storage Complexity: O(1), as the banknotes array has a fixed size of 5.

Code

    class ATM:

    def __init__(self):
        self.banknotes = [0] * 5
        self.values = [20, 50, 100, 200, 500]

    def deposit(self, banknotesCount: list[int]) -> None:
        for i in range(5):
            self.banknotes[i] += banknotesCount[i]

    def withdraw(self, amount: int) -> list[int]:
        result = [0] * 5
        temp_banknotes = self.banknotes[:]  # Create a copy for tentative withdrawal

        for i in range(4, -1, -1):
            num_to_use = min(amount // self.values[i], temp_banknotes[i])
            result[i] = num_to_use
            amount -= num_to_use * self.values[i]
            temp_banknotes[i] -= num_to_use

        if amount == 0:
            self.banknotes = temp_banknotes
            return result
        else:
            return [-1]

More from this blog

C

Chatmagic blog

2894 posts