leetcode

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

1686.cpp (614B)


      1 class Solution {
      2   public:
      3     int stoneGameVI(const vector<int> &aliceValues, const vector<int> &bobValues) {
      4         const int n = size(aliceValues);
      5         vector<int> idx(n);
      6 
      7         iota(begin(idx), end(idx), 0);
      8         sort(begin(idx), end(idx),
      9              [&](int a, int b) { return aliceValues[a] + bobValues[a] > aliceValues[b] + bobValues[b]; });
     10 
     11         int alice = 0, bob = 0, turn = 1;
     12         for (const int i : idx) {
     13             (turn ? alice : bob) += turn ? aliceValues[i] : bobValues[i];
     14             turn = !turn;
     15         }
     16         return alice == bob ? 0 : alice > bob ? 1 : -1;
     17     }
     18 };