2581 Divide Players Into Teams Of Equal Skill
2581 Divide Players Into Teams Of Equal Skill
Divide Players Into Teams of Equal Skill 
You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.
The chemistry of a team is equal to the product of the skills of the players on that team.
Return the sum of the chemistry of all the teams, or return *-1 if there is no way to divide the players into teams such that the total skill of each team is equal.*
Example 1:
1
2
3
4
5
6
7
**Input:** skill = [3,2,5,1,3,4]
**Output:** 22
**Explanation:**
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
Example 2:
1
2
3
4
5
6
7
**Input:** skill = [3,4]
**Output:** 12
**Explanation:**
The two players form a team with a total skill of 7.
The chemistry of the team is 3 * 4 = 12.
Example 3:
1
2
3
4
5
6
**Input:** skill = [1,1,2,3]
**Output:** -1
**Explanation:**
There is no way to divide the players into teams such that the total skill of each team is equal.
Constraints:
1
2
3
2 <= skill.length <= 105
skill.length is even.
1 <= skill[i] <= 1000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def dividePlayers(self, skill: List[int]) -> int:
s = sum(skill)
each_sum = 2 * (s / len(skill))
if each_sum != int(each_sum) :
return -1
each_sum = int(each_sum)
hash = {}
for idx,k in enumerate(skill):
if k in hash:
hash[k].append(idx)
else:
hash[k] = [idx]
result = 0
print(hash, s, each_sum)
for k,v in hash.items():
if each_sum - k not in hash:
return -1
other_item = hash[each_sum - k]
if each_sum - k == k:
start = 0
while(start < len(v)):
result += (skill[v[start]] * skill[v[start+1]])
start += 2
else:
if len(v) != len(other_item):
return -1
for n1, n2 in zip(v, other_item):
result += (skill[n1]*skill[n2])
hash[k] = []
hash[each_sum - k] = []
return result
This post is licensed under CC BY 4.0 by the author.