Problem

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

https://leetcode.cn/problems/design-an-atm-machine/

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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] <= 10⁹
  • 1 <= amount <= 10⁹
  • 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 10⁹

Test Cases

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class ATM:

def __init__(self):


def deposit(self, banknotesCount: List[int]) -> None:


def withdraw(self, amount: int) -> List[int]:



# Your ATM object will be instantiated and called as such:
# obj = ATM()
# obj.deposit(banknotesCount)
# param_2 = obj.withdraw(amount)
solution_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pytest

from solution import ATM

null = None


@pytest.mark.parametrize('actions, params, expects', [
(
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"],
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]],
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]],
),

(
["ATM", "deposit", "withdraw"],
[[], [[0,2,1,1,0]], [300]],
[null, null, [0,0,1,1,0]],
),
(
["ATM", "deposit", "withdraw"],
[[], [[0,0,0,3,1]], [600]],
[null, null, [-1]],
),
])
@pytest.mark.parametrize('clazz', [ATM])
def test_solution(clazz, actions, params, expects):
sol = None
for action, args, expected in zip(actions, params, expects):
if action == 'ATM':
sol = clazz(*args)
else:
assert getattr(sol, action)(*args) == expected

Thoughts

开始以为类似 322. Coin Change494. Target Sum,结果没那么复杂,是个比较「弱智」的机器。直接用贪心法,从最大面值的钞票开始,如果金额大于钞票面额就优先用此钞票。

存入和取出的时间复杂度都是 O(1),空间复杂度 O(1)

Code

solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ATM:

def __init__(self):
self._notes = [20, 50, 100, 200, 500]
self._counts = [0] * 5

def deposit(self, banknotesCount: list[int]) -> None:
for i, cnt in enumerate(banknotesCount):
self._counts[i] += cnt

def withdraw(self, amount: int) -> list[int]:
used = [0] * 5
for i in range(4, -1, -1):
note = self._notes[i]
if self._counts[i] > 0 and (cnt := amount // note) > 0:
cnt = min(cnt, self._counts[i])
used[i] = cnt
amount -= note * cnt
if amount == 0: break
else:
return [-1]

for i, cnt in enumerate(used):
self._counts[i] -= cnt
return used


# Your ATM object will be instantiated and called as such:
# obj = ATM()
# obj.deposit(banknotesCount)
# param_2 = obj.withdraw(amount)