|
1 | 1 | # [2043.Simple Bank System][title]
|
2 | 2 |
|
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 |
| -
|
6 | 3 | ## 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]`. |
7 | 5 |
|
8 |
| -**Example 1:** |
| 6 | +Execute all the **valid** transactions. A transaction is **valid** if: |
9 | 7 |
|
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. |
14 | 10 |
|
15 |
| -## 题意 |
16 |
| -> ... |
| 11 | +Implement the `Bank` class: |
17 | 12 |
|
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. |
19 | 17 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Simple Bank System |
23 |
| -```go |
24 |
| -``` |
| 18 | +**Example 1:** |
25 | 19 |
|
| 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 | +``` |
26 | 39 |
|
27 | 40 | ## 结语
|
28 | 41 |
|
|
0 commit comments