mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
2491-divide-players-into-teams-of-equal-skill
This commit is contained in:
parent
307f93425a
commit
ac27bdd840
19
2491-divide-players-into-teams-of-equal-skill/soln.py
Normal file
19
2491-divide-players-into-teams-of-equal-skill/soln.py
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
class Solution:
|
||||
# pretty elementary approach
|
||||
# in-place sort, so nlogn time, 1 space
|
||||
def dividePlayers(self, skill: list[int]) -> int:
|
||||
skill.sort()
|
||||
sl = len(skill)
|
||||
total_chem = 0
|
||||
avg_team_skill = -1
|
||||
for i in range((sl // 2) - 1, -1, -1):
|
||||
team_skill = skill[i] + skill[sl - i - 1]
|
||||
team_chem = skill[i] * skill[sl - i - 1]
|
||||
if avg_team_skill == -1:
|
||||
avg_team_skill = team_skill
|
||||
elif team_skill != avg_team_skill:
|
||||
return -1
|
||||
total_chem += team_chem
|
||||
return total_chem
|
||||
# could go for a hashmap/dict approach
|
||||
Loading…
x
Reference in New Issue
Block a user