leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2043.cpp (1048B)


      1 #pragma GCC optimize("fast")
      2 static int _ = []() {
      3     ios_base::sync_with_stdio(false);
      4     cout.tie(NULL);
      5     cin.tie(NULL);
      6     return 0;
      7 }();
      8 
      9 class Bank {
     10     const int n;
     11     vector<long long> balance;
     12 
     13     inline bool valid(int acc) const { return acc >= 1 && acc <= n; }
     14 
     15   public:
     16     Bank(const vector<long long> &balance) : n(balance.size()), balance(balance) {}
     17 
     18     inline bool transfer(int account1, int account2, long long money) {
     19         if (!valid(account1) || !valid(account2)) return false;
     20         if (!withdraw(account1, money)) return false;
     21         if (!deposit(account2, money)) return false;
     22         ;
     23         return true;
     24     }
     25 
     26     inline bool deposit(int account, long long money) {
     27         if (!valid(account)) return false;
     28         balance[account - 1] += money;
     29         return true;
     30     }
     31 
     32     inline bool withdraw(int account, long long money) {
     33         if (!valid(account)) return false;
     34         if (money > balance[account - 1]) return false;
     35         balance[account - 1] -= money;
     36         return true;
     37     }
     38 };