Skip to content

Commit a441a79

Browse files
committed
Add solution and test-cases for problem 2241
1 parent f03226b commit a441a79

File tree

3 files changed

+108
-25
lines changed

3 files changed

+108
-25
lines changed

leetcode/2201-2300/2241.Design-an-ATM-Machine/README.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [2241.Design an ATM Machine][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
75

8-
**Example 1:**
6+
When withdrawing, the machine prioritizes using banknotes of **larger** values.
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- 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.
9+
- 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.
1410

15-
## 题意
16-
> ...
11+
Implement the ATM class:
1712

18-
## 题解
13+
- `ATM()` Initializes the ATM object.
14+
- `void deposit(int[] banknotesCount)` Deposits new banknotes in the order `$20`, `$50`, `$100`, `$200`, and `$500`.
15+
- `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).
1916

20-
### 思路1
21-
> ...
22-
Design an ATM Machine
23-
```go
24-
```
17+
**Example 1:**
2518

19+
```
20+
Input
21+
["ATM", "deposit", "withdraw", "deposit", "withdraw", "withdraw"]
22+
[[], [[0,0,1,2,1]], [600], [[0,1,0,1,1]], [600], [550]]
23+
Output
24+
[null, null, [0,0,1,0,1], null, [-1], [0,1,0,0,1]]
25+
26+
Explanation
27+
ATM atm = new ATM();
28+
atm.deposit([0,0,1,2,1]); // Deposits 1 $100 banknote, 2 $200 banknotes,
29+
// and 1 $500 banknote.
30+
atm.withdraw(600); // Returns [0,0,1,0,1]. The machine uses 1 $100 banknote
31+
// and 1 $500 banknote. The banknotes left over in the
32+
// machine are [0,0,0,2,0].
33+
atm.deposit([0,1,0,1,1]); // Deposits 1 $50, $200, and $500 banknote.
34+
// The banknotes in the machine are now [0,1,0,3,1].
35+
atm.withdraw(600); // Returns [-1]. The machine will try to use a $500 banknote
36+
// and then be unable to complete the remaining $100,
37+
// so the withdraw request will be rejected.
38+
// Since the request is rejected, the number of banknotes
39+
// in the machine is not modified.
40+
atm.withdraw(550); // Returns [0,1,0,0,1]. The machine uses 1 $50 banknote
41+
// and 1 $500 banknote.
42+
```
2643

2744
## 结语
2845

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type ATM struct {
4+
value [5]int
5+
atm [5]int
6+
}
7+
8+
func Constructor() ATM {
9+
return ATM{
10+
value: [5]int{20, 50, 100, 200, 500},
11+
atm: [5]int{},
12+
}
13+
}
14+
15+
func (this *ATM) Deposit(banknotesCount []int) {
16+
for i := range 5 {
17+
this.atm[i] += banknotesCount[i]
18+
}
19+
}
20+
21+
func (this *ATM) Withdraw(amount int) []int {
22+
index := 4
23+
ret := make([]int, 5)
24+
for ; index >= 0; index-- {
25+
if this.atm[index] != 0 {
26+
break
27+
}
28+
}
29+
if index == -1 {
30+
return []int{-1}
31+
}
32+
source := this.atm
33+
for ; index >= 0 && amount > 0; index-- {
34+
if this.atm[index] > 0 {
35+
c := amount / this.value[index]
36+
use := min(c, this.atm[index])
37+
amount -= use * this.value[index]
38+
this.atm[index] = max(0, this.atm[index]-use)
39+
ret[index] = use
40+
}
41+
}
42+
if amount > 0 {
43+
// 不要见
44+
this.atm = source
45+
return []int{-1}
46+
}
47+
return ret
48+
}
49+
50+
type op struct {
51+
name byte
52+
banknotesCount []int
53+
amount int
54+
}
55+
56+
func Solution(opts []op) [][]int {
57+
var ret [][]int
58+
atm := Constructor()
59+
for _, o := range opts {
60+
if o.name == 'd' {
61+
atm.Deposit(o.banknotesCount)
62+
continue
63+
}
64+
ret = append(ret, atm.Withdraw(o.amount))
65+
}
66+
return ret
567
}

leetcode/2201-2300/2241.Design-an-ATM-Machine/Solution_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []op
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []op{
17+
{'d', []int{0, 0, 1, 2, 1}, 0},
18+
{'w', []int{}, 600},
19+
{'d', []int{0, 1, 0, 1, 1}, 0},
20+
{'w', []int{}, 600},
21+
{'w', []int{}, 550},
22+
}, [][]int{{0, 0, 1, 0, 1}, {-1}, {0, 1, 0, 0, 1}}},
1923
}
2024

2125
// 开始测试
@@ -30,10 +34,10 @@ func TestSolution(t *testing.T) {
3034
}
3135
}
3236

33-
// 压力测试
37+
// 压力测试
3438
func BenchmarkSolution(b *testing.B) {
3539
}
3640

37-
// 使用案列
41+
// 使用案列
3842
func ExampleSolution() {
3943
}

0 commit comments

Comments
 (0)