Skip to content

Commit c783e36

Browse files
committed
Add prompt and solution for LC 2043. Simple Bank System
1 parent 14c789a commit c783e36

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

2043. Simple Bank System.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
2043. Simple Bank System
4+
5+
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].
6+
7+
Execute all the valid transactions. A transaction is valid if:
8+
9+
The given account number(s) are between 1 and n, and
10+
The amount of money withdrawn or transferred from is less than or equal to the balance of the account.
11+
Implement the Bank class:
12+
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.
17+
18+
19+
Example 1:
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+
39+
40+
Constraints:
41+
n == balance.length
42+
1 <= n, account, account1, account2 <= 105
43+
0 <= balance[i], money <= 1012
44+
At most 104 calls will be made to each function transfer, deposit, withdraw.
45+
46+
*/
47+
48+
class Bank {
49+
constructor(balances) {
50+
this.balances = balances;
51+
}
52+
53+
_accountHasInsufficientFunds = (account, minBalance) => this._getBalance(account) < minBalance;
54+
_creditAccount = (account, amount) => (this.balances[account - 1] += amount);
55+
_debitAccount = (account, amount) => (this.balances[account - 1] -= amount);
56+
_getBalance = (account) => this.balances[account - 1];
57+
_isInvalidAccount = (account) => account <= 0 || account > this.balances.length;
58+
59+
transfer = (fromAccount, toAccount, amount) => {
60+
if (this._isInvalidAccount(fromAccount)) return false;
61+
if (this._isInvalidAccount(toAccount)) return false;
62+
if (this._accountHasInsufficientFunds(fromAccount, amount)) return false;
63+
this._debitAccount(fromAccount, amount);
64+
this._creditAccount(toAccount, amount);
65+
return true;
66+
};
67+
68+
deposit = (account, amount) => {
69+
if (this._isInvalidAccount(account)) return false;
70+
this._creditAccount(account, amount);
71+
return true;
72+
};
73+
74+
withdraw = (account, amount) => {
75+
if (this._isInvalidAccount(account)) return false;
76+
if (this._accountHasInsufficientFunds(account, amount)) return false;
77+
this._debitAccount(account, amount);
78+
return true;
79+
};
80+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ LeetCode Profile: https://leetcode.com/timothyshores/
8080
|[1920. Build Array From Permutation](https://leetcode.com/problems/build-array-from-permutation/)|Easy|[JavaScript](1920.%20Build%20Array%20From%20Permutation.js)
8181
|[1929. Concatenation of Array](https://leetcode.com/problems/concatenation-of-array/)|Easy|[JavaScript](1929.%20Concatenation%20of%20Array.js)
8282
|[2006. Count Number of Pairs With Absolute Difference K](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/)|Easy|[JavaScript](2006.%2Count%2Number%2of%2Pairs%2With%2Absolute%2Difference%2K.js)
83-
|[2011. Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|Easy|[JavaScript](2011.%20Final%20Value%20of%20Variable%20After%20Performing%20Operations.js)
83+
|[2011. Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/)|Easy|[JavaScript](2011.%20Final%20Value%20of%20Variable%20After%20Performing%20Operations.js)
84+
|[2043. Simple Bank System](https://leetcode.com/problems/simple-bank-system/)|Medium|[JavaScript](2043.%20Simple%20Bank%20System.js)
8485
|[2103. Rings and Rods](https://leetcode.com/problems/rings-and-rods/)|Easy|[JavaScript](2103.%20Rings%20and%20Rods.js)
8586
|[2108. Find First Palindromic String in the Array](https://leetcode.com/problems/find-first-palindromic-string-in-the-array/)|Easy|[JavaScript](2108.%20Find%20First%20Palindromic%20String%20in%20the%20Array.js)
8687
|[2114. Maximum Number of Words Found In Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/)|Easy|[JavaScript](2114.%20Maximum%20Number%20of%20Words%20Found%20In%20Sentences.js)

0 commit comments

Comments
 (0)