lc2241. 设计一个 atm 机器

题目链接:LC2241. 设计一个 ATM 机器 2241. 设计一个 ATM 机器

题解

按题意模拟,可能写的有点复杂了。。。

参考代码

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
34
35
class ATM:

def __init__(self):
self.banknotes = {}
self.a = [20, 50, 100, 200, 500]
for i in [20, 50, 100, 200, 500]:
self.banknotes[i] = 0

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

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

ans = []
tmp = amount
# print(self.banknotes)
for i in range(4, -1, -1):
x = min(int(tmp/self.a[i]), self.banknotes[self.a[i]])
# self.banknotes[self.a[i]] -= x
# print(self.a[i], x, tmp)
ans.append(x)
tmp -= x * self.a[i]
if tmp != 0:
return [-1]
ans = ans[::-1]
for i in range(5):
self.banknotes[self.a[i]] -= ans[i]
return ans


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

lc2241. 设计一个 atm 机器

https://blog.xiang578.com/problem/lc2241.html

作者

Ryen Xiang

发布于

2025-01-05

更新于

2025-01-05

许可协议


网络回响

评论