leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2043.cpp (1048B)
0 #pragma GCC optimize("fast") 1 static int _ = []() { 2 ios_base::sync_with_stdio(false); 3 cout.tie(NULL); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Bank { 9 const int n; 10 vector<long long> balance; 11 12 inline bool valid(int acc) const { return acc >= 1 && acc <= n; } 13 14 public: 15 Bank(const vector<long long> &balance) : n(balance.size()), balance(balance) {} 16 17 inline bool transfer(int account1, int account2, long long money) { 18 if (!valid(account1) || !valid(account2)) return false; 19 if (!withdraw(account1, money)) return false; 20 if (!deposit(account2, money)) return false; 21 ; 22 return true; 23 } 24 25 inline bool deposit(int account, long long money) { 26 if (!valid(account)) return false; 27 balance[account - 1] += money; 28 return true; 29 } 30 31 inline bool withdraw(int account, long long money) { 32 if (!valid(account)) return false; 33 if (money > balance[account - 1]) return false; 34 balance[account - 1] -= money; 35 return true; 36 } 37 };