|
| 1 | +<h2><a href="https://leetcode.com/problems/simple-bank-system">2169. Simple Bank System</a></h2><h3>Medium</h3><hr><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> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input</strong> |
| 24 | +["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] |
| 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> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>n == balance.length</code></li> |
| 47 | + <li><code>1 <= n, account, account1, account2 <= 10<sup>5</sup></code></li> |
| 48 | + <li><code>0 <= balance[i], money <= 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> |
0 commit comments