LeetCode 2043: Simple Bank System Solution
Master LeetCode problem 2043 (Simple Bank System), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2043. Simple Bank System
Problem Explanation
Explanation
To solve this problem, we can create a Bank class that stores the account balances in an array. We need to implement three methods: transfer, deposit, and withdraw. Each method should check if the accounts involved are valid and if the transaction amount is valid based on the account balance. We need to handle possible edge cases like invalid accounts or insufficient balance.
Algorithm:
- Initialize the
Bankclass with the given initial balance array. - In the
transfermethod, check if the accounts are valid and if the transfer amount is less than or equal to the balance of the source account. - Update the balances of the source and destination accounts accordingly.
- In the
depositmethod, check if the account is valid and simply add the deposit amount to the account balance. - In the
withdrawmethod, check if the account is valid and if the withdrawal amount is less than or equal to the account balance. - Update the balance by subtracting the withdrawal amount.
Time Complexity:
- For each method (transfer, deposit, withdraw), the time complexity is O(1) as we are directly accessing the elements in the balance array.
Space Complexity:
- The space complexity is O(n) to store the initial balances of all accounts.
Solution Code
class Bank {
long[] balances;
public Bank(long[] balance) {
this.balances = balance;
}
public boolean transfer(int account1, int account2, long money) {
if (account1 <= 0 || account1 > balances.length || account2 <= 0 || account2 > balances.length || balances[account1 - 1] < money) {
return false;
}
balances[account1 - 1] -= money;
balances[account2 - 1] += money;
return true;
}
public boolean deposit(int account, long money) {
if (account <= 0 || account > balances.length) {
return false;
}
balances[account - 1] += money;
return true;
}
public boolean withdraw(int account, long money) {
if (account <= 0 || account > balances.length || balances[account - 1] < money) {
return false;
}
balances[account - 1] -= money;
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2043 (Simple Bank System)?
This page provides optimized solutions for LeetCode problem 2043 (Simple Bank System) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2043 (Simple Bank System)?
The time complexity for LeetCode 2043 (Simple Bank System) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2043 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2043 in Java, C++, or Python.