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 n
accounts numbered from 1
to n
. The initial balance of each account is stored in a 0-indexed integer array balance
, with the (i + 1)th
account having an initial balance of balance[i]
.
Execute all the valid transactions. A transaction is valid if:
1
and n
, andImplement the Bank
class:
Bank(long[] balance)
Initializes the object with the 0-indexed integer array balance
.boolean transfer(int account1, int account2, long money)
Transfers money
dollars from the account numbered account1
to the account numbered account2
. Return true
if the transaction was successful, false
otherwise.boolean deposit(int account, long money)
Deposit money
dollars into the account numbered account
. Return true
if the transaction was successful, false
otherwise.boolean withdraw(int account, long money)
Withdraw money
dollars from the account numbered account
. Return true
if the transaction was successful, false
otherwise.
Example 1:
Input ["Bank", "withdraw", "transfer", "deposit", "transfer", "withdraw"] [[[10, 100, 20, 50, 30]], [3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]] Output [null, true, true, true, false, false] Explanation Bank bank = new Bank([10, 100, 20, 50, 30]); bank.withdraw(3, 10); // return true, account 3 has a balance of $20, so it is valid to withdraw $10. // Account 3 has $20 - $10 = $10. bank.transfer(5, 1, 20); // return true, account 5 has a balance of $30, so it is valid to transfer $20. // Account 5 has $30 - $20 = $10, and account 1 has $10 + $20 = $30. bank.deposit(5, 20); // return true, it is valid to deposit $20 to account 5. // Account 5 has $10 + $20 = $30. bank.transfer(3, 4, 15); // return false, the current balance of account 3 is $10, // so it is invalid to transfer $15 from it. bank.withdraw(10, 50); // return false, it is invalid because account 10 does not exist.
Constraints:
n == balance.length
1 <= n, account, account1, account2 <= 105
0 <= balance[i], money <= 1012
104
calls will be made to each function transfer
, deposit
, withdraw
.This problem involves designing a Bank
class to manage bank accounts and their transactions. The core idea is to simulate the bank's operations using an array to store account balances. Let's break down the solution and its complexities.
Data Structure:
The Bank
class uses a single array (balance
) to store the balance of each account. The index of the array represents the account number (minus 1 for 0-based indexing), and the value at that index represents the account's balance.
Methods:
__init__(self, balance: List[int])
(Constructor): This initializes the Bank
object with the initial balances of all accounts. It stores the balance array and its length (n
) as member variables for efficient access.
transfer(self, account1: int, account2: int, money: int) -> bool
: This method transfers money
from account1
to account2
. It performs the following checks:
account1
and account2
are valid account numbers (within the range 1 to n
).account1
has enough money to transfer.money
from account1
's balance and adds it to account2
's balance. It returns True
for a successful transaction and False
otherwise.deposit(self, account: int, money: int) -> bool
: This deposits money
into account
. It checks for account existence and then updates the balance if valid. It returns True
for success and False
for failure (invalid account).
withdraw(self, account: int, money: int) -> bool
: This withdraws money
from account
. It checks for account existence and sufficient funds before updating the balance and returning True
or False
.
Time and Space Complexity:
Time Complexity: All three methods (transfer
, deposit
, withdraw
) have a time complexity of O(1) because they involve only constant-time operations (array access, arithmetic operations, and comparisons). The initialization in the constructor is O(n) because it iterates through the input balance
array once.
Space Complexity: The space complexity is O(n), where n is the number of accounts, because the balance
array stores n account balances.
Code Examples (Python):
The Python code provided in the original response effectively implements this approach. The code in other languages (Java, C++, Go, TypeScript, Rust) follows the same logic and principles, differing only in syntax and specific language features. They all exhibit the same time and space complexity as the Python solution.
Example Usage (Python):
balance = [10, 100, 20, 50, 30]
bank = Bank(balance)
print(bank.withdraw(3, 10)) # True
print(bank.balance) # [10, 100, 10, 50, 30]
print(bank.transfer(5, 1, 20)) # True
print(bank.balance) # [30, 100, 10, 50, 10]
print(bank.deposit(5, 20)) # True
print(bank.balance) # [30, 100, 10, 50, 30]
print(bank.transfer(3, 4, 15)) # False because account 3 doesn't have enough money
print(bank.withdraw(10, 50)) # False because account 10 doesn't exist
In summary, this problem is solved efficiently using a simple array-based approach with a linear space complexity and constant-time operations for each transaction method. The code is straightforward to implement in various programming languages.