Leetcode Problem 2241. Design an ATM Machine

2241. Design an ATM Machine

Leetcode Solutions

Greedy Withdrawal with Larger Banknotes First

  1. Initialize an array banknotes to store the count of each banknote denomination.
  2. Implement the deposit method to increment the counts of banknotes based on the input array.
  3. Implement the withdraw method as follows:
    • Initialize an array result to store the count of banknotes to be withdrawn.
    • Iterate over the banknote denominations in descending order.
    • For each denomination, calculate the maximum number of banknotes that can be used without exceeding the withdrawal amount.
    • Subtract the value of the used banknotes from the withdrawal amount.
    • If the withdrawal amount becomes zero, update the banknotes array and return result.
    • If the withdrawal amount is not zero after using all denominations, return [-1] to indicate failure.
UML Thumbnail

Check Then Withdraw

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...