leetcode

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

2491.cpp (690B)


      1 class Solution {
      2   public:
      3     long long dividePlayers(const vector<int> &skill) const {
      4         static int count[1001];
      5         memset(count, 0x00, sizeof(count));
      6 
      7         const int sum = accumulate(begin(skill), end(skill), 0);
      8         const int n = size(skill), goal = sum / (n / 2);
      9 
     10         if (goal * n / 2 != sum) return -1;
     11 
     12         long long res = 0, cnt = 0;
     13         for (const int n : skill) {
     14             const int target = goal - n;
     15             if (!count[target])
     16                 count[n]++;
     17             else {
     18                 count[target]--;
     19                 res += target * n;
     20                 cnt += 2;
     21             }
     22         }
     23 
     24         return cnt == n ? res : -1;
     25     }
     26 };