Validate Stateful Account Transactions. Implement Bank for accounts numbered 1 through n, initialized from the zero-based balance array. transfer moves money between two valid accounts only if the source can cover it. deposit adds money to a valid account; withdraw removes it only if covered. Return true for a successful operation and false otherwise, with no state change on failure.
### Problem
Implement a `Bank` object for accounts numbered from `1` through `n`. The constructor receives the initial balances, with array position `i` holding the balance of account `i + 1`. `transfer(account1, account2, money)` moves money between two valid accounts if the source balance is sufficient. `deposit(account, money)` adds money to a valid account, and `withdraw(account, money)` removes money from a valid account if its balance is sufficient. Return `true` exactly when the requested operation succeeds. A failed operation must leave all balances unchanged.
```cpp
class Bank {
public:
Bank(vector<long long>& balance);
bool transfer(int account1, int account2, long long money);
bool deposit(int account, long long money);
bool withdraw(int account, long long money);
};
```
### Examples
**Example 1:**
```text
balance = [8,2]
Calls: transfer(1,2,5), withdraw(1,4), deposit(2,1)
Results: true, false, true
Explanation: Balances become [3,7], remain unchanged on the failed withdrawal, then become [3,8].
```
**Example 2:**
```text
balance = [4]
Calls: transfer(1,1,4), withdraw(2,0), withdraw(1,4)
Results: true, false, true
Explanation: A covered self-transfer preserves balance; invalid account 2 is rejected even for zero money.
```
### Constraints
- `1 <= n <= 10^5`; provided account numbers lie in `[1,10^5]` and may exceed n.
- Initial balances and money lie in `[0,10^12]`.
- At most `10^4` calls to each transaction method.
### Clarifying Questions
Account numbers are one-based. Zero-money operations still validate accounts and balances. The object owns persistent balances.
```hint Inspect the structure
Which checks must happen before subtracting anything in a transfer?
```
### Reasoning checkpoints
- Match the success and failure conditions for every operation without partially changing a failed transaction.
- Handle one-based account labels, zero amounts and transfers to the same account.
- Keep accumulated balances representable throughout a long sequence of calls.
### Optional extensions
Which invariant distinguishes a transfer from a deposit or withdrawal?
Overview: Validate Stateful Account Transactions. Implement Bank for accounts numbered 1 through n, initialized from the zero-based balance array. transfer moves money between two valid accounts only if the source can cover it. deposit adds money to a valid account; withdraw removes it only if covered. Return true for a successful operation and false otherwise, with no state change on failure.
Implement a Bank object for accounts numbered from 1 through n. The constructor receives the initial balances, with array position i holding the balance of account i + 1. transfer(account1, account2, money) moves money between two valid accounts if the source balance is sufficient. deposit(account, money) adds money to a valid account, and withdraw(account, money) removes money from a valid account if its balance is sufficient. Return true exactly when the requested operation succeeds. A failed operation must leave all balances unchanged.
class Bank {
public:
Bank(vector<long long>& balance);
bool transfer(int account1, int account2, long long money);
bool deposit(int account, long long money);
bool withdraw(int account, long long money);
};
Examples
Example 1:
balance = [8,2]
Calls: transfer(1,2,5), withdraw(1,4), deposit(2,1)
Results: true, false, true
Explanation: Balances become [3,7], remain unchanged on the failed withdrawal, then become [3,8].
Example 2:
balance = [4]
Calls: transfer(1,1,4), withdraw(2,0), withdraw(1,4)
Results: true, false, true
Explanation: A covered self-transfer preserves balance; invalid account 2 is rejected even for zero money.
Constraints
1 <= n <= 10^5
; provided account numbers lie in
[1,10^5]
and may exceed n.
Initial balances and money lie in
[0,10^12]
.
At most
10^4
calls to each transaction method.
Clarifying Questions Guidance
Account numbers are one-based. Zero-money operations still validate accounts and balances. The object owns persistent balances.
Reasoning checkpoints
Match the success and failure conditions for every operation without partially changing a failed transaction.
Handle one-based account labels, zero amounts and transfers to the same account.
Keep accumulated balances representable throughout a long sequence of calls.
Optional extensions
Which invariant distinguishes a transfer from a deposit or withdrawal?