|
| 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