Skip to content

Commit bf7a199

Browse files
committed
[LeetCode Sync] Runtime - 14 ms (97.41%), Memory - 47.7 MB (82.51%)
1 parent 19364f2 commit bf7a199

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>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 <code>n</code> accounts numbered from <code>1</code> to <code>n</code>. The initial balance of each account is stored in a <strong>0-indexed</strong> integer array <code>balance</code>, with the <code>(i + 1)<sup>th</sup></code> account having an initial balance of <code>balance[i]</code>.</p>
2+
3+
<p>Execute all the <strong>valid</strong> transactions. A transaction is <strong>valid</strong> if:</p>
4+
5+
<ul>
6+
<li>The given account number(s) are between <code>1</code> and <code>n</code>, and</li>
7+
<li>The amount of money withdrawn or transferred from is <strong>less than or equal</strong> to the balance of the account.</li>
8+
</ul>
9+
10+
<p>Implement the <code>Bank</code> class:</p>
11+
12+
<ul>
13+
<li><code>Bank(long[] balance)</code> Initializes the object with the <strong>0-indexed</strong> integer array <code>balance</code>.</li>
14+
<li><code>boolean transfer(int account1, int account2, long money)</code> Transfers <code>money</code> dollars from the account numbered <code>account1</code> to the account numbered <code>account2</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li>
15+
<li><code>boolean deposit(int account, long money)</code> Deposit <code>money</code> dollars into the account numbered <code>account</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li>
16+
<li><code>boolean withdraw(int account, long money)</code> Withdraw <code>money</code> dollars from the account numbered <code>account</code>. Return <code>true</code> if the transaction was successful, <code>false</code> otherwise.</li>
17+
</ul>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input</strong>
24+
[&quot;Bank&quot;, &quot;withdraw&quot;, &quot;transfer&quot;, &quot;deposit&quot;, &quot;transfer&quot;, &quot;withdraw&quot;]
25+
[[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]
26+
<strong>Output</strong>
27+
[null, true, true, true, false, false]
28+
29+
<strong>Explanation</strong>
30+
Bank bank = new Bank([10, 100, 20, 50, 30]);
31+
bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10.
32+
// Account 3 has $20 - $10 = $10.
33+
bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20.
34+
// Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30.
35+
bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5.
36+
// Account 5 has $10 + $20 = $30.
37+
bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10,
38+
// so it is invalid to transfer $15 from it.
39+
bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>n == balance.length</code></li>
47+
<li><code>1 &lt;= n, account, account1, account2 &lt;= 10<sup>5</sup></code></li>
48+
<li><code>0 &lt;= balance[i], money &lt;= 10<sup>12</sup></code></li>
49+
<li>At most <code>10<sup>4</sup></code> calls will be made to <strong>each</strong> function <code>transfer</code>, <code>deposit</code>, <code>withdraw</code>.</li>
50+
</ul>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Bank:
2+
3+
def __init__(self, balance: List[int]):
4+
self.bank = balance
5+
self.n = len(balance) + 1
6+
7+
def transfer(self, account1: int, account2: int, money: int) -> bool:
8+
if self.bank and account1 <= self.n and account2 <= self.n and self.bank[account1 - 1] >= money:
9+
self.bank[account2 - 1] += money
10+
self.bank[account1 - 1] -= money
11+
return True
12+
return False
13+
14+
def deposit(self, account: int, money: int) -> bool:
15+
if self.bank and account <= self.n:
16+
self.bank[account - 1] += money
17+
return True
18+
return False
19+
20+
def withdraw(self, account: int, money: int) -> bool:
21+
if self.bank and account <= self.n and self.bank[account - 1] >= money:
22+
self.bank[account -1] -= money
23+
return True
24+
return False
25+
26+
27+
# Your Bank object will be instantiated and called as such:
28+
# obj = Bank(balance)
29+
# param_1 = obj.transfer(account1,account2,money)
30+
# param_2 = obj.deposit(account,money)
31+
# param_3 = obj.withdraw(account,money)

0 commit comments

Comments
 (0)