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)


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