2241. Design an ATM Machine LeetCode Solution

In this guide, you will get 2241. Design an ATM Machine LeetCode Solution with the best time and space complexity. The solution to Design an ATM Machine problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Design an ATM Machine solution in C++
  4. Design an ATM Machine solution in Java
  5. Design an ATM Machine solution in Python
  6. Additional Resources
2241. Design an ATM Machine LeetCode Solution image

Problem Statement of Design an ATM Machine

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).

See also  2207. Maximize Number of Subsequences in a String LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2241. Design an ATM Machine LeetCode Solution in C++

class ATM {
 public:
  ATM() : bank(5) {}

  void deposit(vector<int> banknotesCount) {
    for (int i = 0; i < 5; ++i)
      bank[i] += banknotesCount[i];
  }

  vector<int> withdraw(int amount) {
    vector<int> withdrew(5);

    for (int i = 4; i >= 0; --i) {
      withdrew[i] = min(bank[i], static_cast<long>(amount) / banknotes[i]);
      amount -= withdrew[i] * banknotes[i];
    }

    if (amount > 0)
      return {-1};

    for (int i = 0; i < 5; ++i)
      bank[i] -= withdrew[i];
    return withdrew;
  }

 private:
  vector<int> banknotes{20, 50, 100, 200, 500};
  vector<long> bank;
};
/* code provided by PROGIEZ */

2241. Design an ATM Machine LeetCode Solution in Java

class ATM {
  public void deposit(int[] banknotesCount) {
    for (int i = 0; i < 5; ++i)
      bank[i] += banknotesCount[i];
  }

  public int[] withdraw(int amount) {
    int[] withdrew = new int[5];

    for (int i = 4; i >= 0; --i) {
      withdrew[i] = (int) Math.min(bank[i], (long) amount / banknotes[i]);
      amount -= withdrew[i] * banknotes[i];
    }

    if (amount > 0)
      return new int[] {-1};

    for (int i = 0; i < 5; ++i)
      bank[i] -= withdrew[i];
    return withdrew;
  }

  private int[] banknotes = {20, 50, 100, 200, 500};
  private long[] bank = new long[5];
}
// code provided by PROGIEZ

2241. Design an ATM Machine LeetCode Solution in Python

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

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

  def withdraw(self, amount: int) -> list[int]:
    withdrew = [0] * 5

    for i in reversed(range(5)):
      withdrew[i] = min(self.bank[i], amount // self.banknotes[i])
      amount -= withdrew[i] * self.banknotes[i]

    if amount:
      return [-1]

    for i in range(5):
      self.bank[i] -= withdrew[i]
    return withdrew
# code by PROGIEZ

Additional Resources

See also  1992. Find All Groups of Farmland LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.