Skip to content

Commit 9aaa4b4

Browse files
authored
Create divide-players-into-teams-of-equal-skill.cpp
1 parent 9f98408 commit 9aaa4b4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
long long dividePlayers(vector<int>& skill) {
8+
const int64_t target = accumulate(cbegin(skill), cend(skill), 0ll) / (size(skill) / 2);
9+
unordered_map<int, int> cnt;
10+
for (const auto& s : skill) {
11+
++cnt[s];
12+
}
13+
int64_t result = 0;
14+
for (const auto& [k, v] : cnt) {
15+
if (!cnt.count(target - k) || cnt[target - k] != cnt[k]) {
16+
return -1;
17+
}
18+
result += k * (target - k) * v;
19+
}
20+
return result / 2;
21+
}
22+
};

0 commit comments

Comments
 (0)