Advertisement

Design an ATM Machine - LeetCode 2241 Solution

Design an ATM Machine - Complete Solution Guide

Design an ATM Machine is LeetCode problem 2241, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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

Detailed Explanation

The problem asks us to design an ATM machine with deposit and withdraw functionalities. The ATM stores banknotes of denominations $20, $50, $100, $200, and $500. The `deposit` function adds banknotes to the ATM's inventory. The `withdraw` function aims to dispense the requested amount using the largest possible denominations first, updating the ATM's inventory accordingly. If the ATM cannot dispense the exact amount, it should return `[-1]` and leave the inventory unchanged. The key is to prioritize larger denominations during withdrawal and ensure we have enough of each denomination to fulfill the request.

Solution Approach

The solution implements the ATM class with `deposit` and `withdraw` methods. The `deposit` method simply adds the deposited banknotes to the ATM's existing counts. The `withdraw` method uses a greedy approach. It iterates through the denominations from largest to smallest, determining the maximum number of banknotes of each denomination that can be used without exceeding the remaining amount. If, after iterating through all denominations, the remaining amount is zero, the withdrawal is successful, and the ATM's inventory is updated. Otherwise, the withdrawal fails, and `[-1]` is returned.

Step-by-Step Algorithm

  1. Step 1: Initialize the ATM with the denominations [20, 50, 100, 200, 500] and an array to store the count of each denomination, initially set to 0.
  2. Step 2: Implement the `deposit` function. For each denomination, add the deposited amount to the corresponding count in the `counts` array.
  3. Step 3: Implement the `withdraw` function. Create an array `withdraw_counts` to track the banknotes to dispense.
  4. Step 4: Iterate through the denominations array from largest to smallest (500 to 20).
  5. Step 5: For each denomination, calculate how many banknotes can be dispensed without exceeding the `remaining_amount` and without exceeding the available number of banknotes of that denomination.
  6. Step 6: Update `withdraw_counts` with the number of banknotes to dispense for the current denomination and reduce the `remaining_amount`.
  7. Step 7: After iterating through all denominations, check if `remaining_amount` is 0. If it is, update the ATM's `counts` array by subtracting the dispensed banknotes.
  8. Step 8: If `remaining_amount` is 0, return the `withdraw_counts` array. Otherwise, return `[-1]`.

Key Insights

  • Insight 1: The problem emphasizes a greedy approach for the withdraw operation, prioritizing larger denominations first.
  • Insight 2: The ATM's state (number of banknotes of each denomination) needs to be maintained consistently after each operation.
  • Insight 3: Careful handling of edge cases is crucial to avoid errors and ensure correct operation, particularly when the exact amount cannot be withdrawn.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Array, Greedy, Design.

Companies

Asked at: Yandex.