304 North Cardinal St.
Dorchester Center, MA 02124

# Design an ATM Machine LeetCode Solution

## Problem – Design an ATM Machine LeetCode Solution

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]], , [[0,1,0,1,1]], , ]
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`.

### Design an ATM Machine LeetCode Solution in Python3

``````class ATM:
bank, val =  * 5, [20, 50, 100, 200, 500]

def deposit(self, banknotesCount: List[int]) -> None:
self.bank = [a + b for a, b in zip(self.bank, banknotesCount)]

def withdraw(self, amount: int) -> List[int]:
take =  * 5
for i in range(4, -1, -1):
take[i] = min(self.bank[i], amount // self.val[i])
amount -= take[i] * self.val[i]
if amount == 0:
self.bank = [a - b for a, b in zip(self.bank, take)]
return [-1] if amount else take
``````

### Design an ATM Machine LeetCode Solution in C++

``````class ATM {
public:
long long bank = {}, val = {20, 50, 100, 200, 500};
void deposit(vector<int> banknotesCount) {
for (int i = 0; i < 5; ++i)
bank[i] += banknotesCount[i];
}
vector<int> withdraw(int amount) {
vector<int> take(5);
for (int i = 4; i >= 0; --i) {
take[i] = min(bank[i], amount / val[i]);
amount -= take[i] * val[i];
}
if (amount)
return { -1 };
for (int i = 0; i < 5; ++i)
bank[i] -= take[i];
return take;
}
};
``````

### Design an ATM Machine LeetCode Solution in Java

``````import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class ATM {

long[] denoms = {20, 50, 100, 200, 500};
long[] stores;

public ATM() {
stores = new long;
}

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

public int[] withdraw(int amount) {
long[] ans = new long;
int index = 4;
while (amount > 0 && index >= 0) {
long takethismany = Math.min(amount / denoms[index], stores[index]);
ans[index] = takethismany;
amount -= takethismany * denoms[index];
index--;
}

if (amount != 0) {
return new int[]{-1};
} else {
for (int i = 0; i < 5; i++) {
stores[i] -= ans[i];
}
return Arrays.stream(ans).mapToInt(i -> (int) i).toArray();
}
}

}
``````

### Design an ATM Machine LeetCode Solution in Python

``````class ATM:

def __init__(self):
self.arr = [0, 0, 0, 0, 0]
self.d = {0:20, 1:50, 2: 100, 3: 200, 4: 500}

def deposit(self, banknotesCount: List[int]) -> None:
for i in range(len(banknotesCount)):
self.arr[i] += banknotesCount[i]

def withdraw(self, amount: int) -> List[int]:
length = len(self.arr)
ans = [0 for i in range(length)]

k = length - 1
# We start with k = 4 as the machine prioritizes using banknotes of larger values.
while k >= 0 and amount > 0:
note = self.d[k]
if amount >= note and self.arr[k]:
x = amount // note
num = min(self.arr[k], x)
ans[k] = num
amount -= (note * num)
k -= 1

if amount == 0:
for i in range(length):
self.arr[i] -= ans[i]
return ans
else:
return [-1]
``````
##### Design an ATM Machine LeetCode Solution Review:

In our experience, we suggest you solve this Design an ATM Machine LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Design an ATM Machine LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Design an ATM Machine LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions