Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Topic describes

This is 2043 on LeetCode. Simple banking system, medium difficulty.

Tag: “Simulation”

Your task is to design a program for a popular bank that automates all incoming transactions (transfers, deposits, and withdrawals).

The bank has NNN accounts numbered from 111 to NNN. The initial balance of each account is stored in an integer array balance with subscripts starting from 000, where the initial balance of the (I +1)(I +1)(I +1) account is balance[I]balance[I]balance[I].

You execute all valid transactions. The transaction is valid if all of the following conditions are met:

  • The number of specified accounts is between 111 and NNN, and
  • The amount of money required to withdraw or transfer is less than or equal to the account balance.

Implement Bank class:

  • Bank(long[] balance)Use subscripts from
    0 0
    An array of integers to start withbalanceInitialize the object.
  • boolean transfer(int account1, int account2, long money)From the Numbers foraccount1The account direction number ofaccount2Account transfermoneyThe dollar. Returns if the transaction is successfultrueOtherwise, returnfalse
  • boolean deposit(int account, long money)The Numbers foraccountAccount depositmoneyThe dollar. Returns if the transaction is successfultrue; Otherwise, returnfalse
  • boolean withdraw(int account, long money)From the Numbers foraccountWithdrawal from your accountmoneyThe dollar. Returns if the transaction is successfultrue; Otherwise, returnfalse

Example:

Input:  ["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] [[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, Bank Bank = new Bank([10, 100, 20, 50, 30]); Bank Bank = new Bank([10, 100, 20, 50, 30]) bank.withdraw(3, 10); // Return true, account 3 has a balance of $20, so you can withdraw $10. // Account 3 balances $20 - $10 = $10. bank.transfer(5, 1, 20); // Return true, account 5 has a balance of $30, so $20 can be transferred. // Account 5 has a balance of $30 - $20 = $10 and account 1 has a balance of $10 + $20 = $30. bank.deposit(5, 20); // Return true to deposit $20 into account 5. // The balance of account 5 is $10 + $20 = $30. bank.transfer(3, 4, 15); // Return false, the current balance of account 3 is $10. // Can't transfer $15. bank.withdraw(10, 50); // Return false, the transaction is invalid because account 10 does not exist.Copy the code

Tip:


  • n = = b a l a n c e . l e n g t h n == balance.length

  • 1 < = n . a c c o u n t . a c c o u n t 1 . a c c o u n t 2 < = 1 0 5 1 <= n, account, account_1, account_2 <= 10^5

  • 0 < = b a l a n c e [ i ] . m o n e y < = 1 0 12 0 <= balance[i], money <= 10^{12}
  • transfer.deposit.withdrawThree functions, each called at most
    1 0 4 10 ^ 4

simulation

Carry out the simulation according to the meaning of the question.

Code:

class Bank {
    long[] val;
    public Bank(long[] balance) {
        val = balance;
    }

    boolean check(int account) {
        return 1 <= account && account <= val.length;
    }
    
    public boolean transfer(int a, int b, long c) {
        if(! check(a) || ! check(b))return false;
        if (val[a - 1] >= c) {
            val[a - 1] -= c; val[b - 1] += c;
            return true;
        } 
        return false;
    }
    
    public boolean deposit(int a, long c) {
        if(! check(a))return false;
        val[a - 1] += c;
        return true;
    }
    
    public boolean withdraw(int a, long c) {
        if(! check(a))return false;
        if (val[a - 1] >= c) {
            val[a - 1] -= c;
            return true;
        }
        return false; }}Copy the code
  • Time complexity: O(1)O(1)O(1)
  • Space complexity: O(n)O(n)O(n)

The last

This is the No.2043 of our “Brush through LeetCode” series. The series started on 2021/01/01.

In this series of articles, in addition to explaining how to solve the problem, I will also present the most concise code possible. If a general solution is involved, the corresponding code template will also be used.

In order to facilitate the students to debug and submit the code on the computer, I set up the relevant warehouse: github.com/SharingSour… .

In the repository, you’ll see links to the series of articles, the corresponding code for the series of articles, the LeetCode source links, and other preferred solutions.