2043. Simple Bank System Medium
1/**
2 * [2043] Simple Bank System
3 *
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].
5 * Execute all the valid transactions. A transaction is valid if:
6 *
7 * The given account number(s) are between 1 and n, and
8 * The amount of money withdrawn or transferred from is less than or equal to the balance of the account.
9 *
10 * Implement the Bank class:
11 *
12 * Bank(long[] balance) Initializes the object with the 0-indexed integer array balance.
13 * 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.
14 * boolean deposit(int account, long money) Deposit money dollars into the account numbered account. Return true if the transaction was successful, false otherwise.
15 * boolean withdraw(int account, long money) Withdraw money dollars from the account numbered account. Return true if the transaction was successful, false otherwise.
16 *
17 *
18 * Example 1:
19 *
20 * Input
21 * ["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"]
22 * [[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
23 * Output
24 * [null, true, true, true, false, false]
25 * Explanation
26 * Bank bank = new Bank([10, 100, 20, 50, 30]);
27 * bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
28 * // Account 3 has $20 - $10 = $10.
29 * bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
30 * // Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
31 * bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
32 * // Account 5 has $10 + $20 = $30.
33 * bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
34 * // so it is invalid to transfer $15 from it.
35 * bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
36 *
37 *
38 * Constraints:
39 *
40 * n == balance.length
41 * 1 <= n, account, account1, account2 <= 10^5
42 * 0 <= balance[i], money <= 10^12
43 * At most 10^4 calls will be made to each function transfer, deposit, withdraw.
44 *
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/simple-bank-system/
49// discuss: https://leetcode.com/problems/simple-bank-system/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53struct Bank {
54 false
55 }
56
57
58/**
59 * `&self` means the method takes an immutable reference.
60 * If you need a mutable reference, change it to `&mut self` instead.
61 */
62impl Bank {
63
64 fn new(balance: Vec<i64>) -> Self {
65
66 }
67
68 fn transfer(&self, account1: i32, account2: i32, money: i64) -> bool {
69
70 }
71
72 fn deposit(&self, account: i32, money: i64) -> bool {
73
74 }
75
76 fn withdraw(&self, account: i32, money: i64) -> bool {
77
78 }
79}
80
81/**
82 * Your Bank object will be instantiated and called as such:
83 * let obj = Bank::new(balance);
84 * let ret_1: bool = obj.transfer(account1, account2, money);
85 * let ret_2: bool = obj.deposit(account, money);
86 * let ret_3: bool = obj.withdraw(account, money);
87 */
88
89// submission codes end
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn test_2043() {
97 }
98}
99
Back
© 2025 bowen.ge All Rights Reserved.