-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_players_into_teams_of_equal_skill.py
56 lines (40 loc) · 1.55 KB
/
divide_players_into_teams_of_equal_skill.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
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:
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:
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:
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:
2 <= skill.length <= 105
skill.length is even.
1 <= skill[i] <= 1000
"""
class Solution:
def dividePlayers(self, skill: List[int]) -> int:
skill.sort()
left = 0
right = len(skill) - 1
ans = 0
total = skill[left] + skill[right]
while left <= right:
if skill[left] + skill[right] != total:
return -1
ans = ans + (skill[left] * skill[right])
left = left + 1
right = right - 1
return ans