forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-bank-system.cpp
38 lines (32 loc) · 969 Bytes
/
simple-bank-system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Time: ctor: O(1)
// transer: O(1)
// deposit: O(1)
// withdraw: O(1)
// Space: O(1)
class Bank {
public:
Bank(vector<long long>& balance) : balance_(balance) {
}
bool transfer(int account1, int account2, long long money) {
if (1 <= account2 && account2 <= size(balance_) && withdraw(account1, money)) {
return deposit(account2, money);
}
return false;
}
bool deposit(int account, long long money) {
if (1 <= account && account <= size(balance_)) {
balance_[account - 1] += money;
return true;
}
return false;
}
bool withdraw(int account, long long money) {
if (1 <= account && account <= size(balance_) && balance_[account - 1] >= money) {
balance_[account - 1] -= money;
return true;
}
return false;
}
private:
vector<long long>& balance_;
};