Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ddfd211

Browse files
committed
Add solution #2043
1 parent 81c0c8e commit ddfd211

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,704 LeetCode solutions in JavaScript
1+
# 1,705 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1566,6 +1566,7 @@
15661566
2039|[The Time When the Network Becomes Idle](./solutions/2039-the-time-when-the-network-becomes-idle.js)|Medium|
15671567
2040|[Kth Smallest Product of Two Sorted Arrays](./solutions/2040-kth-smallest-product-of-two-sorted-arrays.js)|Hard|
15681568
2042|[Check if Numbers Are Ascending in a Sentence](./solutions/2042-check-if-numbers-are-ascending-in-a-sentence.js)|Easy|
1569+
2043|[Simple Bank System](./solutions/2043-simple-bank-system.js)|Medium|
15691570
2047|[Number of Valid Words in a Sentence](./solutions/2047-number-of-valid-words-in-a-sentence.js)|Easy|
15701571
2053|[Kth Distinct String in an Array](./solutions/2053-kth-distinct-string-in-an-array.js)|Medium|
15711572
2071|[Maximum Number of Tasks You Can Assign](./solutions/2071-maximum-number-of-tasks-you-can-assign.js)|Hard|

solutions/2043-simple-bank-system.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* 2043. Simple Bank System
3+
* https://leetcode.com/problems/simple-bank-system/
4+
* Difficulty: Medium
5+
*
6+
* You have been tasked with writing a program for a popular bank that will automate all its
7+
* incoming transactions (transfer, deposit, and withdraw). The bank has n accounts numbered
8+
* from 1 to n. The initial balance of each account is stored in a 0-indexed integer array
9+
* balance, with the (i + 1)th account having an initial balance of balance[i].
10+
*
11+
* Execute all the valid transactions. A transaction is valid if:
12+
* - The given account number(s) are between 1 and n, and
13+
* - The amount of money withdrawn or transferred from is less than or equal to the balance
14+
* of the account.
15+
*
16+
* Implement the Bank class:
17+
* - Bank(long[] balance) Initializes the object with the 0-indexed integer array balance.
18+
* - boolean transfer(int account1, int account2, long money) Transfers money dollars from the
19+
* account numbered account1 to the account numbered account2. Return true if the transaction
20+
* was successful, false otherwise.
21+
* - boolean deposit(int account, long money) Deposit money dollars into the account numbered
22+
* account. Return true if the transaction was successful, false otherwise.
23+
* - boolean withdraw(int account, long money) Withdraw money dollars from the account numbered
24+
* account. Return true if the transaction was successful, false otherwise.
25+
*/
26+
27+
/**
28+
* @param {number[]} balance
29+
*/
30+
var Bank = function(balance) {
31+
this.accounts = balance;
32+
};
33+
34+
/**
35+
* @param {number} account1
36+
* @param {number} account2
37+
* @param {number} money
38+
* @return {boolean}
39+
*/
40+
Bank.prototype.transfer = function(account1, account2, money) {
41+
if (account1 > this.accounts.length || account2 > this.accounts.length) return false;
42+
if (this.accounts[account1 - 1] < money) return false;
43+
44+
this.accounts[account1 - 1] -= money;
45+
this.accounts[account2 - 1] += money;
46+
return true;
47+
};
48+
49+
/**
50+
* @param {number} account
51+
* @param {number} money
52+
* @return {boolean}
53+
*/
54+
Bank.prototype.deposit = function(account, money) {
55+
if (account > this.accounts.length) return false;
56+
57+
this.accounts[account - 1] += money;
58+
return true;
59+
};
60+
61+
/**
62+
* @param {number} account
63+
* @param {number} money
64+
* @return {boolean}
65+
*/
66+
Bank.prototype.withdraw = function(account, money) {
67+
if (account > this.accounts.length || this.accounts[account - 1] < money) return false;
68+
69+
this.accounts[account - 1] -= money;
70+
return true;
71+
};

0 commit comments

Comments
 (0)