@@ -60,6 +60,14 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
60
60
61
61
<!-- 这里可写通用的实现逻辑 -->
62
62
63
+ ** 方法一:模拟**
64
+
65
+ 我们用一个数组 $d$ 记录钞票面额,用一个数组 $cnt$ 记录每种面额的钞票数量。
66
+
67
+ 对于 ` deposit ` 操作,我们只需要将对应面额的钞票数量加上即可。时间复杂度 $O(1)$。
68
+
69
+ 对于 ` withdraw ` 操作,我们从大到小枚举每种面额的钞票,取出尽可能多且不超过 $amount$ 的钞票,然后将 $amount$ 减去取出的钞票面额之和,如果最后 $amount$ 仍大于 $0$,说明无法取出 $amount$ 的钞票,返回 $-1$ 即可。否则,返回取出的钞票数量即可。时间复杂度 $O(1)$。
70
+
63
71
<!-- tabs:start -->
64
72
65
73
### ** Python3**
@@ -70,22 +78,22 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
70
78
class ATM :
71
79
def __init__ (self ):
72
80
self .cnt = [0 ] * 5
73
- self .m = [500 , 200 , 100 , 50 , 20 ]
81
+ self .d = [20 , 50 , 100 , 200 , 500 ]
74
82
75
83
def deposit (self , banknotesCount : List[int ]) -> None :
76
- for i, v in enumerate (banknotesCount[:: - 1 ] ):
84
+ for i, v in enumerate (banknotesCount):
77
85
self .cnt[i] += v
78
86
79
87
def withdraw (self , amount : int ) -> List[int ]:
80
88
ans = [0 ] * 5
81
- for i, n in enumerate ( self .cnt ):
82
- ans[i] = min (amount // self .m [i], n )
83
- amount -= self .m [i] * ans [i]
89
+ for i in range ( 4 , - 1 , - 1 ):
90
+ ans[i] = min (amount // self .d [i], self .cnt[i] )
91
+ amount -= ans [i] * self .d [i]
84
92
if amount > 0 :
85
93
return [- 1 ]
86
94
for i, v in enumerate (ans):
87
95
self .cnt[i] -= v
88
- return ans[:: - 1 ]
96
+ return ans
89
97
90
98
91
99
# Your ATM object will be instantiated and called as such:
@@ -99,7 +107,133 @@ class ATM:
99
107
<!-- 这里可写当前语言的特殊实现逻辑 -->
100
108
101
109
``` java
110
+ class ATM {
111
+ private long [] cnt = new long [5 ];
112
+ private int [] d = {20 , 50 , 100 , 200 , 500 };
113
+
114
+ public ATM () {
115
+
116
+ }
117
+
118
+ public void deposit (int [] banknotesCount ) {
119
+ for (int i = 0 ; i < banknotesCount. length; ++ i) {
120
+ cnt[i] += banknotesCount[i];
121
+ }
122
+ }
123
+
124
+ public int [] withdraw (int amount ) {
125
+ int [] ans = new int [5 ];
126
+ for (int i = 4 ; i >= 0 ; -- i) {
127
+ ans[i] = (int ) Math . min(amount / d[i], cnt[i]);
128
+ amount -= ans[i] * d[i];
129
+ }
130
+ if (amount > 0 ) {
131
+ return new int [] {- 1 };
132
+ }
133
+ for (int i = 0 ; i < 5 ; ++ i) {
134
+ cnt[i] -= ans[i];
135
+ }
136
+ return ans;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Your ATM object will be instantiated and called as such:
142
+ * ATM obj = new ATM();
143
+ * obj.deposit(banknotesCount);
144
+ * int[] param_2 = obj.withdraw(amount);
145
+ */
146
+ ```
147
+
148
+ ### ** C++**
149
+
150
+ ``` cpp
151
+ class ATM {
152
+ public:
153
+ ATM() {
154
+
155
+ }
156
+
157
+ void deposit (vector<int > banknotesCount) {
158
+ for (int i = 0; i < banknotesCount.size(); ++i) {
159
+ cnt[ i] += banknotesCount[ i] ;
160
+ }
161
+ }
162
+
163
+ vector<int> withdraw(int amount) {
164
+ vector<int> ans(5);
165
+ for (int i = 4; ~i; --i) {
166
+ ans[i] = min(1ll * amount / d[i], cnt[i]);
167
+ amount -= ans[i] * d[i];
168
+ }
169
+ if (amount > 0) {
170
+ return {-1};
171
+ }
172
+ for (int i = 0; i < 5; ++i) {
173
+ cnt[i] -= ans[i];
174
+ }
175
+ return ans;
176
+ }
177
+
178
+ private:
179
+ long long cnt[ 5] = {0};
180
+ int d[ 5] = {20, 50, 100, 200, 500};
181
+ };
182
+
183
+ /**
184
+ * Your ATM object will be instantiated and called as such:
185
+ * ATM* obj = new ATM();
186
+ * obj->deposit(banknotesCount);
187
+ * vector<int > param_2 = obj->withdraw(amount);
188
+ * /
189
+ ```
102
190
191
+ ### **Go**
192
+
193
+ ```go
194
+ type ATM struct {
195
+ d [5]int
196
+ cnt [5]int
197
+ }
198
+
199
+ func Constructor() ATM {
200
+ return ATM{[5]int{20, 50, 100, 200, 500}, [5]int{}}
201
+ }
202
+
203
+ func (this *ATM) Deposit(banknotesCount []int) {
204
+ for i, v := range banknotesCount {
205
+ this.cnt[i] += v
206
+ }
207
+ }
208
+
209
+ func (this *ATM) Withdraw(amount int) []int {
210
+ ans := make([]int, 5)
211
+ for i := 4; i >= 0; i-- {
212
+ ans[i] = min(amount/this.d[i], this.cnt[i])
213
+ amount -= ans[i] * this.d[i]
214
+ }
215
+ if amount > 0 {
216
+ return []int{-1}
217
+ }
218
+ for i, v := range ans {
219
+ this.cnt[i] -= v
220
+ }
221
+ return ans
222
+ }
223
+
224
+ func min(a, b int) int {
225
+ if a < b {
226
+ return a
227
+ }
228
+ return b
229
+ }
230
+
231
+ /**
232
+ * Your ATM object will be instantiated and called as such:
233
+ * obj := Constructor();
234
+ * obj.Deposit(banknotesCount);
235
+ * param_2 := obj.Withdraw(amount);
236
+ */
103
237
```
104
238
105
239
### ** TypeScript**
0 commit comments