-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
60 lines (53 loc) · 1.43 KB
/
solution.cpp
File metadata and controls
60 lines (53 loc) · 1.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <vector>
using namespace std;
/*
Simple Bank System - C++
*/
class Bank
{
private:
vector<long long> bal; // store balances as 0-indexed
// helper to check valid 1-indexed account
inline bool valid(int account)
{
return account >= 1 && account <= (int)bal.size();
}
public:
Bank(vector<long long> &balance) : bal(balance) {}
bool transfer(int account1, int account2, long long money)
{
if (!valid(account1) || !valid(account2))
return false;
int a = account1 - 1;
int b = account2 - 1;
if (bal[a] < money)
return false; // insufficient funds
bal[a] -= money;
bal[b] += money;
return true;
}
bool deposit(int account, long long money)
{
if (!valid(account))
return false;
bal[account - 1] += money;
return true;
}
bool withdraw(int account, long long money)
{
if (!valid(account))
return false;
int a = account - 1;
if (bal[a] < money)
return false;
bal[a] -= money;
return true;
}
};
/**
* Your Bank object will be instantiated and called as such:
* Bank* obj = new Bank(balance);
* bool param_1 = obj->transfer(account1,account2,money);
* bool param_2 = obj->deposit(account,money);
* bool param_3 = obj->withdraw(account,money);
*/