From 3f6aed5558a2e1c9064f97613ac4bb616f72db27 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 17 Mar 2025 09:39:48 +0800 Subject: [PATCH] Add solution and test-cases for problem 2043 --- .../2043.Simple-Bank-System/README.md | 45 +++++++---- .../2043.Simple-Bank-System/Solution.go | 75 ++++++++++++++++++- .../2043.Simple-Bank-System/Solution_test.go | 27 ++++--- 3 files changed, 118 insertions(+), 29 deletions(-) diff --git a/leetcode/2001-2100/2043.Simple-Bank-System/README.md b/leetcode/2001-2100/2043.Simple-Bank-System/README.md index c44fe7784..2f3fb85fb 100755 --- a/leetcode/2001-2100/2043.Simple-Bank-System/README.md +++ b/leetcode/2001-2100/2043.Simple-Bank-System/README.md @@ -1,28 +1,41 @@ # [2043.Simple Bank System][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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]`. -**Example 1:** +Execute all the **valid** transactions. A transaction is **valid** if: -``` -Input: a = "11", b = "1" -Output: "100" -``` +- The given account number(s) are between `1` and `n`, and +- The amount of money withdrawn or transferred from is **less than or equal** to the balance of the account. -## 题意 -> ... +Implement the `Bank` class: -## 题解 +- `Bank(long[] balance)` Initializes the object with the **0-indexed** integer array `balance`. +- `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. +- `boolean deposit(int account, long money)` Deposit `money` dollars into the account numbered account. Return `true` if the transaction was successful, `false` otherwise. +- `boolean withdraw(int account, long money)` Withdraw `money` dollars from the account numbered `account`. Return `true` if the transaction was successful, `false` otherwise. -### 思路1 -> ... -Simple Bank System -```go -``` +**Example 1:** +``` +Input +["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] +[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]] +Output +[null, true, true, true, false, false] + +Explanation +Bank bank = new Bank([10, 100, 20, 50, 30]); +bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10. + // Account 3 has $20 - $10 = $10. +bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20. + // Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30. +bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5. + // Account 5 has $10 + $20 = $30. +bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10, + // so it is invalid to transfer $15 from it. +bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist. +``` ## 结语 diff --git a/leetcode/2001-2100/2043.Simple-Bank-System/Solution.go b/leetcode/2001-2100/2043.Simple-Bank-System/Solution.go index d115ccf5e..ffe6b73c5 100644 --- a/leetcode/2001-2100/2043.Simple-Bank-System/Solution.go +++ b/leetcode/2001-2100/2043.Simple-Bank-System/Solution.go @@ -1,5 +1,76 @@ package Solution -func Solution(x bool) bool { - return x +type Bank struct { + b []int64 + ok int +} + +func Constructor(balance []int64) Bank { + // 0, 1, 2, 3, 4, 5 + b := make([]int64, len(balance)+1) + for i := 1; i <= len(balance); i++ { + b[i] = balance[i-1] + } + return Bank{b: b, ok: len(balance)} +} + +func (this *Bank) validateAccount(a int) bool { + return a >= 1 && a <= this.ok +} + +func (this *Bank) Transfer(account1 int, account2 int, money int64) bool { + if !this.validateAccount(account1) || !this.validateAccount(account2) { + return false + } + have := this.b[account1] + if have >= money { + this.b[account1] -= money + this.b[account2] += money + return true + } + return false +} + +func (this *Bank) Deposit(account int, money int64) bool { + if !this.validateAccount(account) { + return false + } + this.b[account] += money + return true +} + +func (this *Bank) Withdraw(account int, money int64) bool { + // 0 ,1, 2, 3 + if !this.validateAccount(account) { + return false + } + have := this.b[account] + if have >= money { + this.b[account] -= money + return true + } + return false +} + +type action struct { + a string + from, to int + money int64 +} + +func Solution(balance []int64, actions []action) []bool { + c := Constructor(balance) + ans := make([]bool, len(actions)) + for i, ac := range actions { + if ac.a == "t" { + ans[i] = c.Transfer(ac.from, ac.to, ac.money) + continue + } + if ac.a == "d" { + ans[i] = c.Deposit(ac.from, ac.money) + continue + } + ans[i] = c.Withdraw(ac.from, ac.money) + } + return ans } diff --git a/leetcode/2001-2100/2043.Simple-Bank-System/Solution_test.go b/leetcode/2001-2100/2043.Simple-Bank-System/Solution_test.go index 14ff50eb4..c97375770 100644 --- a/leetcode/2001-2100/2043.Simple-Bank-System/Solution_test.go +++ b/leetcode/2001-2100/2043.Simple-Bank-System/Solution_test.go @@ -9,31 +9,36 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + balances []int64 + actions []action + expect []bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int64{10, 100, 20, 50, 30}, []action{ + {"w", 3, 0, 10}, + {"t", 5, 1, 20}, + {"d", 5, 0, 20}, + {"t", 3, 4, 15}, + {"w", 10, 0, 50}, + }, []bool{true, true, true, false, false}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.balances, c.actions) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.balances, c.actions) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }