Skip to content

Commit fd7c89f

Browse files
authored
feat: update solutions to lc problem: No.2241 (#3925)
No.2241.Design an ATM Machine
1 parent 3b89cf8 commit fd7c89f

File tree

7 files changed

+137
-122
lines changed

7 files changed

+137
-122
lines changed

solution/2200-2299/2241.Design an ATM Machine/README.md

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
7878

7979
### 方法一:模拟
8080

81-
我们用一个数组 $d$ 记录钞票面额,用一个数组 $cnt$ 记录每种面额的钞票数量。
81+
我们用一个数组 $\textit{d}$ 记录钞票面额,用一个数组 $\textit{cnt}$ 记录每种面额的钞票数量。
8282

8383
对于 `deposit` 操作,我们只需要将对应面额的钞票数量加上即可。时间复杂度 $O(1)$。
8484

85-
对于 `withdraw` 操作,我们从大到小枚举每种面额的钞票,取出尽可能多且不超过 $amount$ 的钞票,然后将 $amount$ 减去取出的钞票面额之和,如果最后 $amount$ 仍大于 $0$,说明无法取出 $amount$ 的钞票,返回 $-1$ 即可。否则,返回取出的钞票数量即可。时间复杂度 $O(1)$。
85+
对于 `withdraw` 操作,我们从大到小枚举每种面额的钞票,取出尽可能多且不超过 $\textit{amount}$ 的钞票,然后将 $\textit{amount}$ 减去取出的钞票面额之和,如果最后 $\textit{amount}$ 仍大于 $0$,说明无法取出 $\textit{amount}$ 的钞票,返回 $-1$ 即可。否则,返回取出的钞票数量即可。时间复杂度 $O(1)$。
8686

8787
<!-- tabs:start -->
8888

@@ -91,22 +91,23 @@ atm.withdraw(550); // 返回 [0,1,0,0,1] ,机器会返回 1 张 $50 的
9191
```python
9292
class ATM:
9393
def __init__(self):
94-
self.cnt = [0] * 5
9594
self.d = [20, 50, 100, 200, 500]
95+
self.m = len(self.d)
96+
self.cnt = [0] * self.m
9697

9798
def deposit(self, banknotesCount: List[int]) -> None:
98-
for i, v in enumerate(banknotesCount):
99-
self.cnt[i] += v
99+
for i, x in enumerate(banknotesCount):
100+
self.cnt[i] += x
100101

101102
def withdraw(self, amount: int) -> List[int]:
102-
ans = [0] * 5
103-
for i in range(4, -1, -1):
103+
ans = [0] * self.m
104+
for i in reversed(range(self.m)):
104105
ans[i] = min(amount // self.d[i], self.cnt[i])
105106
amount -= ans[i] * self.d[i]
106107
if amount > 0:
107108
return [-1]
108-
for i, v in enumerate(ans):
109-
self.cnt[i] -= v
109+
for i, x in enumerate(ans):
110+
self.cnt[i] -= x
110111
return ans
111112

112113

@@ -120,8 +121,9 @@ class ATM:
120121

121122
```java
122123
class ATM {
123-
private long[] cnt = new long[5];
124124
private int[] d = {20, 50, 100, 200, 500};
125+
private int m = d.length;
126+
private long[] cnt = new long[5];
125127

126128
public ATM() {
127129
}
@@ -133,15 +135,15 @@ class ATM {
133135
}
134136

135137
public int[] withdraw(int amount) {
136-
int[] ans = new int[5];
137-
for (int i = 4; i >= 0; --i) {
138+
int[] ans = new int[m];
139+
for (int i = m - 1; i >= 0; --i) {
138140
ans[i] = (int) Math.min(amount / d[i], cnt[i]);
139141
amount -= ans[i] * d[i];
140142
}
141143
if (amount > 0) {
142144
return new int[] {-1};
143145
}
144-
for (int i = 0; i < 5; ++i) {
146+
for (int i = 0; i < m; ++i) {
145147
cnt[i] -= ans[i];
146148
}
147149
return ans;
@@ -171,23 +173,24 @@ public:
171173
}
172174

173175
vector<int> withdraw(int amount) {
174-
vector<int> ans(5);
175-
for (int i = 4; ~i; --i) {
176+
vector<int> ans(m);
177+
for (int i = m - 1; ~i; --i) {
176178
ans[i] = min(1ll * amount / d[i], cnt[i]);
177179
amount -= ans[i] * d[i];
178180
}
179181
if (amount > 0) {
180182
return {-1};
181183
}
182-
for (int i = 0; i < 5; ++i) {
184+
for (int i = 0; i < m; ++i) {
183185
cnt[i] -= ans[i];
184186
}
185187
return ans;
186188
}
187189

188190
private:
189-
long long cnt[5] = {0};
190-
int d[5] = {20, 50, 100, 200, 500};
191+
static constexpr int d[5] = {20, 50, 100, 200, 500};
192+
static constexpr int m = size(d);
193+
long long cnt[m] = {0};
191194
};
192195

193196
/**
@@ -201,32 +204,33 @@ private:
201204
#### Go
202205
203206
```go
204-
type ATM struct {
205-
d [5]int
206-
cnt [5]int
207-
}
207+
var d = [...]int{20, 50, 100, 200, 500}
208+
209+
const m = len(d)
210+
211+
type ATM [m]int
208212
209213
func Constructor() ATM {
210-
return ATM{[5]int{20, 50, 100, 200, 500}, [5]int{}}
214+
return ATM{}
211215
}
212216
213217
func (this *ATM) Deposit(banknotesCount []int) {
214-
for i, v := range banknotesCount {
215-
this.cnt[i] += v
218+
for i, x := range banknotesCount {
219+
this[i] += x
216220
}
217221
}
218222
219223
func (this *ATM) Withdraw(amount int) []int {
220-
ans := make([]int, 5)
221-
for i := 4; i >= 0; i-- {
222-
ans[i] = min(amount/this.d[i], this.cnt[i])
223-
amount -= ans[i] * this.d[i]
224+
ans := make([]int, m)
225+
for i := m - 1; i >= 0; i-- {
226+
ans[i] = min(amount/d[i], this[i])
227+
amount -= ans[i] * d[i]
224228
}
225229
if amount > 0 {
226230
return []int{-1}
227231
}
228-
for i, v := range ans {
229-
this.cnt[i] -= v
232+
for i, x := range ans {
233+
this[i] -= x
230234
}
231235
return ans
232236
}
@@ -242,31 +246,32 @@ func (this *ATM) Withdraw(amount int) []int {
242246
#### TypeScript
243247

244248
```ts
249+
const d: number[] = [20, 50, 100, 200, 500];
250+
const m = d.length;
251+
245252
class ATM {
246253
private cnt: number[];
247-
private d: number[];
248254

249255
constructor() {
250-
this.cnt = [0, 0, 0, 0, 0];
251-
this.d = [20, 50, 100, 200, 500];
256+
this.cnt = Array(m).fill(0);
252257
}
253258

254259
deposit(banknotesCount: number[]): void {
255-
for (let i = 0; i < banknotesCount.length; i++) {
260+
for (let i = 0; i < banknotesCount.length; ++i) {
256261
this.cnt[i] += banknotesCount[i];
257262
}
258263
}
259264

260265
withdraw(amount: number): number[] {
261-
let ans = [0, 0, 0, 0, 0];
262-
for (let i = 4; i >= 0; i--) {
263-
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
264-
amount -= ans[i] * this.d[i];
266+
const ans: number[] = Array(m).fill(0);
267+
for (let i = m - 1; i >= 0; --i) {
268+
ans[i] = Math.min(Math.floor(amount / d[i]), this.cnt[i]);
269+
amount -= ans[i] * d[i];
265270
}
266271
if (amount > 0) {
267272
return [-1];
268273
}
269-
for (let i = 0; i < ans.length; i++) {
274+
for (let i = 0; i < m; ++i) {
270275
this.cnt[i] -= ans[i];
271276
}
272277
return ans;

solution/2200-2299/2241.Design an ATM Machine/README_EN.md

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknot
8484

8585
### Solution 1: Simulation
8686

87-
We use an array $d$ to record the denominations of the bills and an array $cnt$ to record the number of bills for each denomination.
87+
We use an array $\textit{d}$ to record the denominations of the bills and an array $\textit{cnt}$ to record the number of bills for each denomination.
8888

89-
For the `deposit` operation, we only need to add the number of bills for the corresponding denomination. The time complexity is $O(1)$.
89+
For the `deposit` operation, we simply add the number of bills to the corresponding denomination. The time complexity is $O(1)$.
9090

91-
For the `withdraw` operation, we enumerate the bills from largest to smallest denomination, taking out as many bills as possible without exceeding the $amount$. Then, we subtract the total value of the withdrawn bills from $amount$. If $amount$ is still greater than $0$ at the end, it means it's not possible to withdraw the $amount$ with the available bills, and we return $-1$. Otherwise, we return the number of bills withdrawn. The time complexity is $O(1)$.
91+
For the `withdraw` operation, we enumerate the bills from largest to smallest denomination, taking out as many bills as possible without exceeding $\textit{amount}$. We then subtract the total value of the withdrawn bills from $\textit{amount}$. If $\textit{amount}$ is still greater than $0$ at the end, it means we cannot withdraw the requested amount, and we return $-1$. Otherwise, we return the number of withdrawn bills. The time complexity is $O(1)$.
9292

9393
<!-- tabs:start -->
9494

@@ -97,22 +97,23 @@ For the `withdraw` operation, we enumerate the bills from largest to smallest de
9797
```python
9898
class ATM:
9999
def __init__(self):
100-
self.cnt = [0] * 5
101100
self.d = [20, 50, 100, 200, 500]
101+
self.m = len(self.d)
102+
self.cnt = [0] * self.m
102103

103104
def deposit(self, banknotesCount: List[int]) -> None:
104-
for i, v in enumerate(banknotesCount):
105-
self.cnt[i] += v
105+
for i, x in enumerate(banknotesCount):
106+
self.cnt[i] += x
106107

107108
def withdraw(self, amount: int) -> List[int]:
108-
ans = [0] * 5
109-
for i in range(4, -1, -1):
109+
ans = [0] * self.m
110+
for i in reversed(range(self.m)):
110111
ans[i] = min(amount // self.d[i], self.cnt[i])
111112
amount -= ans[i] * self.d[i]
112113
if amount > 0:
113114
return [-1]
114-
for i, v in enumerate(ans):
115-
self.cnt[i] -= v
115+
for i, x in enumerate(ans):
116+
self.cnt[i] -= x
116117
return ans
117118

118119

@@ -126,8 +127,9 @@ class ATM:
126127

127128
```java
128129
class ATM {
129-
private long[] cnt = new long[5];
130130
private int[] d = {20, 50, 100, 200, 500};
131+
private int m = d.length;
132+
private long[] cnt = new long[5];
131133

132134
public ATM() {
133135
}
@@ -139,15 +141,15 @@ class ATM {
139141
}
140142

141143
public int[] withdraw(int amount) {
142-
int[] ans = new int[5];
143-
for (int i = 4; i >= 0; --i) {
144+
int[] ans = new int[m];
145+
for (int i = m - 1; i >= 0; --i) {
144146
ans[i] = (int) Math.min(amount / d[i], cnt[i]);
145147
amount -= ans[i] * d[i];
146148
}
147149
if (amount > 0) {
148150
return new int[] {-1};
149151
}
150-
for (int i = 0; i < 5; ++i) {
152+
for (int i = 0; i < m; ++i) {
151153
cnt[i] -= ans[i];
152154
}
153155
return ans;
@@ -177,23 +179,24 @@ public:
177179
}
178180

179181
vector<int> withdraw(int amount) {
180-
vector<int> ans(5);
181-
for (int i = 4; ~i; --i) {
182+
vector<int> ans(m);
183+
for (int i = m - 1; ~i; --i) {
182184
ans[i] = min(1ll * amount / d[i], cnt[i]);
183185
amount -= ans[i] * d[i];
184186
}
185187
if (amount > 0) {
186188
return {-1};
187189
}
188-
for (int i = 0; i < 5; ++i) {
190+
for (int i = 0; i < m; ++i) {
189191
cnt[i] -= ans[i];
190192
}
191193
return ans;
192194
}
193195

194196
private:
195-
long long cnt[5] = {0};
196-
int d[5] = {20, 50, 100, 200, 500};
197+
static constexpr int d[5] = {20, 50, 100, 200, 500};
198+
static constexpr int m = size(d);
199+
long long cnt[m] = {0};
197200
};
198201

199202
/**
@@ -207,32 +210,33 @@ private:
207210
#### Go
208211
209212
```go
210-
type ATM struct {
211-
d [5]int
212-
cnt [5]int
213-
}
213+
var d = [...]int{20, 50, 100, 200, 500}
214+
215+
const m = len(d)
216+
217+
type ATM [m]int
214218
215219
func Constructor() ATM {
216-
return ATM{[5]int{20, 50, 100, 200, 500}, [5]int{}}
220+
return ATM{}
217221
}
218222
219223
func (this *ATM) Deposit(banknotesCount []int) {
220-
for i, v := range banknotesCount {
221-
this.cnt[i] += v
224+
for i, x := range banknotesCount {
225+
this[i] += x
222226
}
223227
}
224228
225229
func (this *ATM) Withdraw(amount int) []int {
226-
ans := make([]int, 5)
227-
for i := 4; i >= 0; i-- {
228-
ans[i] = min(amount/this.d[i], this.cnt[i])
229-
amount -= ans[i] * this.d[i]
230+
ans := make([]int, m)
231+
for i := m - 1; i >= 0; i-- {
232+
ans[i] = min(amount/d[i], this[i])
233+
amount -= ans[i] * d[i]
230234
}
231235
if amount > 0 {
232236
return []int{-1}
233237
}
234-
for i, v := range ans {
235-
this.cnt[i] -= v
238+
for i, x := range ans {
239+
this[i] -= x
236240
}
237241
return ans
238242
}
@@ -248,31 +252,32 @@ func (this *ATM) Withdraw(amount int) []int {
248252
#### TypeScript
249253

250254
```ts
255+
const d: number[] = [20, 50, 100, 200, 500];
256+
const m = d.length;
257+
251258
class ATM {
252259
private cnt: number[];
253-
private d: number[];
254260

255261
constructor() {
256-
this.cnt = [0, 0, 0, 0, 0];
257-
this.d = [20, 50, 100, 200, 500];
262+
this.cnt = Array(m).fill(0);
258263
}
259264

260265
deposit(banknotesCount: number[]): void {
261-
for (let i = 0; i < banknotesCount.length; i++) {
266+
for (let i = 0; i < banknotesCount.length; ++i) {
262267
this.cnt[i] += banknotesCount[i];
263268
}
264269
}
265270

266271
withdraw(amount: number): number[] {
267-
let ans = [0, 0, 0, 0, 0];
268-
for (let i = 4; i >= 0; i--) {
269-
ans[i] = Math.min(Math.floor(amount / this.d[i]), this.cnt[i]);
270-
amount -= ans[i] * this.d[i];
272+
const ans: number[] = Array(m).fill(0);
273+
for (let i = m - 1; i >= 0; --i) {
274+
ans[i] = Math.min(Math.floor(amount / d[i]), this.cnt[i]);
275+
amount -= ans[i] * d[i];
271276
}
272277
if (amount > 0) {
273278
return [-1];
274279
}
275-
for (let i = 0; i < ans.length; i++) {
280+
for (let i = 0; i < m; ++i) {
276281
this.cnt[i] -= ans[i];
277282
}
278283
return ans;

0 commit comments

Comments
 (0)