Simple Bank System (LeetCode 2043): Solution and Explanation
Implement transfer, deposit and withdraw on n numbered accounts, refusing any call that names a missing account or overdraws one.
Intuition
Accounts are numbered 1 to n with no gaps, so they map straight onto array indexes: account i is balance[i - 1]. No hash map is needed.
Every operation follows the same shape: check that the accounts exist, check that the paying account has enough money, and only then change balances.
Doing all checks first matters. If a transfer subtracted from account1 and then discovered that account2 does not exist, the bank would lose money.
Approach: Validate, then update
- Store the balances in an array (64-bit, since deposits can push a balance past 2^31).
- transfer(a, b, money): return false if a or b is outside 1..n or balance[a - 1] < money. Otherwise subtract from a, add to b, return true.
- deposit(a, money): return false if a is outside 1..n. Otherwise add and return true.
- withdraw(a, money): return false if a is outside 1..n or balance[a - 1] < money. Otherwise subtract and return true.
Step-by-step example
balance = [10, 100, 20, 50, 30]
- withdraw(3, 10): account 3 has 20 >= 10, so it drops to 10. Return true.
- transfer(5, 1, 20): both accounts exist and account 5 has 30 >= 20. Account 5 drops to 10, account 1 rises to 30. Return true.
- deposit(5, 20): account 5 exists, so it rises to 30. Return true.
- transfer(3, 4, 15): account 3 holds 10 < 15. Return false, nothing changes.
- withdraw(10, 50): there are only 5 accounts. Return false.
- Output: [null, true, true, true, false, false].
Code: Python, Java, C++
1class Bank:
2 def __init__(self, balance: List[int]):
3 self.balance = balance
4 self.n = len(balance)
5
6 def transfer(self, account1: int, account2: int, money: int) -> bool:
7 if not (1 <= account1 <= self.n and 1 <= account2 <= self.n):
8 return False
9 if self.balance[account1 - 1] < money:
10 return False
11 self.balance[account1 - 1] -= money
12 self.balance[account2 - 1] += money
13 return True
14
15 def deposit(self, account: int, money: int) -> bool:
16 if not 1 <= account <= self.n:
17 return False
18 self.balance[account - 1] += money
19 return True
20
21 def withdraw(self, account: int, money: int) -> bool:
22 if not 1 <= account <= self.n:
23 return False
24 if self.balance[account - 1] < money:
25 return False
26 self.balance[account - 1] -= money
27 return True1class Bank {
2 private long[] balance;
3
4 public Bank(int[] balance) {
5 this.balance = new long[balance.length];
6 for (int i = 0; i < balance.length; i++) {
7 this.balance[i] = balance[i];
8 }
9 }
10
11 private boolean valid(int account) {
12 return account >= 1 && account <= balance.length;
13 }
14
15 public boolean transfer(int account1, int account2, long money) {
16 if (!valid(account1) || !valid(account2)) {
17 return false;
18 }
19 if (balance[account1 - 1] < money) {
20 return false;
21 }
22 balance[account1 - 1] -= money;
23 balance[account2 - 1] += money;
24 return true;
25 }
26
27 public boolean deposit(int account, long money) {
28 if (!valid(account)) {
29 return false;
30 }
31 balance[account - 1] += money;
32 return true;
33 }
34
35 public boolean withdraw(int account, long money) {
36 if (!valid(account)) {
37 return false;
38 }
39 if (balance[account - 1] < money) {
40 return false;
41 }
42 balance[account - 1] -= money;
43 return true;
44 }
45}1class Bank {
2 vector<long long> balance;
3
4 bool valid(int account) {
5 return account >= 1 && account <= (int) balance.size();
6 }
7
8public:
9 Bank(vector<int>& balance) {
10 this->balance.assign(balance.begin(), balance.end());
11 }
12
13 bool transfer(int account1, int account2, long long money) {
14 if (!valid(account1) || !valid(account2)) {
15 return false;
16 }
17 if (balance[account1 - 1] < money) {
18 return false;
19 }
20 balance[account1 - 1] -= money;
21 balance[account2 - 1] += money;
22 return true;
23 }
24
25 bool deposit(int account, long long money) {
26 if (!valid(account)) {
27 return false;
28 }
29 balance[account - 1] += money;
30 return true;
31 }
32
33 bool withdraw(int account, long long money) {
34 if (!valid(account)) {
35 return false;
36 }
37 if (balance[account - 1] < money) {
38 return false;
39 }
40 balance[account - 1] -= money;
41 return true;
42 }
43};Time and space complexity
O(1) per call
Each call does a fixed number of comparisons and array updates.
O(n)
One balance per account.
Other approaches
Hash map of balances (time O(1) per call, space O(n))
Map account number to balance. It works the same way, but the dense numbering makes an array simpler and faster.
Common mistakes
- Changing a balance before every check has passed, which leaves the bank in a half-done state on failure.
- Forgetting the 1-based numbering and indexing balance[account] directly.
- Storing balances as 32-bit ints. LeetCode allows balances up to 10^12, and even with small inputs repeated deposits can pass 2^31.
- Treating a transfer from an account to itself as an error. It is valid as long as the account exists and has enough money.
Frequently asked questions
Is a transfer to the same account allowed?
Yes. If the account exists and holds at least money, subtracting and adding the same amount leaves it unchanged and the call returns true.
Is withdrawing 0 valid?
Yes, as long as the account exists. Every balance is at least 0, so the funds check passes.
Why does this version take int amounts?
LeetCode uses 64-bit balances and amounts up to 10^12. Here the inputs stay below 2^31 so the judge can pass them as ints, but the solution still keeps balances in 64-bit integers because sums can grow past that.