Design an ATM Machine
expert · 1010 · lc medium +32 · 44.6% accepted · 311 likes · top 27%
Description
An ATM stores banknotes in 5 denominations: 20, 50, 100, 200, and 500 dollars. It starts empty. Users may deposit or withdraw money.
During withdrawal, the machine always tries larger denominations first.
- For example, to withdraw $300 with 2 $50 notes, 1 $100 note, and 1 $200 note, the machine uses the $100 and $200 notes.
- However, trying to withdraw $600 with 3 $200 notes and 1 $500 note is rejected: the machine commits to the $500 note and cannot complete the remaining $100. It may not skip the $500 in favor of $200 notes.
Implement the ATM class:
- ATM() Initializes the ATM.
- void deposit(int[] banknotesCount) Adds notes in denomination order $20, $50, $100, $200, $500.
- int[] withdraw(int amount) Returns the note counts dispensed in the same denomination order, updating internal counts. Returns [-1] if the amount cannot be fulfilled exactly.
Example 1:
Example 2:
Code