2491-divide-players-into-teams-of-equal-skill

This commit is contained in:
Kaushik Narayan R 2024-10-03 22:38:37 -07:00
parent 307f93425a
commit ac27bdd840

View 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