Skip to content

Commit 3f6aed5

Browse files
committed
Add solution and test-cases for problem 2043
1 parent f0a9eb1 commit 3f6aed5

File tree

3 files changed

+118
-29
lines changed

3 files changed

+118
-29
lines changed

leetcode/2001-2100/2043.Simple-Bank-System/README.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2043.Simple Bank System][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+
You have been tasked with writing a program for a popular bank that will automate all its incoming transactions (transfer, deposit, and withdraw). The bank has `n` accounts numbered from `1` to `n`. The initial balance of each account is stored in a **0-indexed** integer array `balance`, with the `(i + 1)th` account having an initial balance of `balance[i]`.
75

8-
**Example 1:**
6+
Execute all the **valid** transactions. A transaction is **valid** if:
97

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
8+
- The given account number(s) are between `1` and `n`, and
9+
- The amount of money withdrawn or transferred from is **less than or equal** to the balance of the account.
1410

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

18-
## 题解
13+
- `Bank(long[] balance)` Initializes the object with the **0-indexed** integer array `balance`.
14+
- `boolean transfer(int account1, int account2, long money)` Transfers `money` dollars from the account numbered `account1` to the account numbered `account2`. Return `true` if the transaction was successful, `false` otherwise.
15+
- `boolean deposit(int account, long money)` Deposit `money` dollars into the account numbered account. Return `true` if the transaction was successful, `false` otherwise.
16+
- `boolean withdraw(int account, long money)` Withdraw `money` dollars from the account numbered `account`. Return `true` if the transaction was successful, `false` otherwise.
1917

20-
### 思路1
21-
> ...
22-
Simple Bank System
23-
```go
24-
```
18+
**Example 1:**
2519

20+
```
21+
Input
22+
["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
23+
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
24+
Output
25+
[null, true, true, true, false, false]
26+
27+
Explanation
28+
Bank bank = new Bank([10, 100, 20, 50, 30]);
29+
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
30+
// Account 3 has $20 - $10 = $10.
31+
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
32+
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
33+
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
34+
// Account 5 has $10 + $20 = $30.
35+
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
36+
// so it is invalid to transfer $15 from it.
37+
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
type Bank struct {
4+
b []int64
5+
ok int
6+
}
7+
8+
func Constructor(balance []int64) Bank {
9+
// 0, 1, 2, 3, 4, 5
10+
b := make([]int64, len(balance)+1)
11+
for i := 1; i <= len(balance); i++ {
12+
b[i] = balance[i-1]
13+
}
14+
return Bank{b: b, ok: len(balance)}
15+
}
16+
17+
func (this *Bank) validateAccount(a int) bool {
18+
return a >= 1 && a <= this.ok
19+
}
20+
21+
func (this *Bank) Transfer(account1 int, account2 int, money int64) bool {
22+
if !this.validateAccount(account1) || !this.validateAccount(account2) {
23+
return false
24+
}
25+
have := this.b[account1]
26+
if have >= money {
27+
this.b[account1] -= money
28+
this.b[account2] += money
29+
return true
30+
}
31+
return false
32+
}
33+
34+
func (this *Bank) Deposit(account int, money int64) bool {
35+
if !this.validateAccount(account) {
36+
return false
37+
}
38+
this.b[account] += money
39+
return true
40+
}
41+
42+
func (this *Bank) Withdraw(account int, money int64) bool {
43+
// 0 ,1, 2, 3
44+
if !this.validateAccount(account) {
45+
return false
46+
}
47+
have := this.b[account]
48+
if have >= money {
49+
this.b[account] -= money
50+
return true
51+
}
52+
return false
53+
}
54+
55+
type action struct {
56+
a string
57+
from, to int
58+
money int64
59+
}
60+
61+
func Solution(balance []int64, actions []action) []bool {
62+
c := Constructor(balance)
63+
ans := make([]bool, len(actions))
64+
for i, ac := range actions {
65+
if ac.a == "t" {
66+
ans[i] = c.Transfer(ac.from, ac.to, ac.money)
67+
continue
68+
}
69+
if ac.a == "d" {
70+
ans[i] = c.Deposit(ac.from, ac.money)
71+
continue
72+
}
73+
ans[i] = c.Withdraw(ac.from, ac.money)
74+
}
75+
return ans
576
}

leetcode/2001-2100/2043.Simple-Bank-System/Solution_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,36 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
balances []int64
14+
actions []action
15+
expect []bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int64{10, 100, 20, 50, 30}, []action{
18+
{"w", 3, 0, 10},
19+
{"t", 5, 1, 20},
20+
{"d", 5, 0, 20},
21+
{"t", 3, 4, 15},
22+
{"w", 10, 0, 50},
23+
}, []bool{true, true, true, false, false}},
1924
}
2025

2126
// 开始测试
2227
for i, c := range cases {
2328
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
29+
got := Solution(c.balances, c.actions)
2530
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
31+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
32+
c.expect, got, c.balances, c.actions)
2833
}
2934
})
3035
}
3136
}
3237

33-
// 压力测试
38+
// 压力测试
3439
func BenchmarkSolution(b *testing.B) {
3540
}
3641

37-
// 使用案列
42+
// 使用案列
3843
func ExampleSolution() {
3944
}

0 commit comments

Comments
 (0)