Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Python/Divide_Players_Into_Teams_Of_Equal_Skills.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from typing import List

class Solution:
def dividePlayers(self, skill: List[int]) -> int:
# Step 1: Sort the skill array
skill.sort()

total_skill = skill[0] + skill[-1] # Required sum for each pair
# Step 2: Calculate the required sum for each pair
total_skill = skill[0] + skill[-1]
chemistry_sum = 0

# Step 2: Pair players using two pointers
for i in range(len(skill) // 2):
# Check if the sum of current pair matches the required total_skill
if skill[i] + skill[-i - 1] != total_skill:
# Step 3: Pair players using two pointers
n = len(skill)
for i in range(n // 2):
# Check if the sum of the current pair matches the required total_skill
if skill[i] + skill[n - 1 - i] != total_skill:
return -1 # Invalid configuration, return -1

# Calculate the chemistry (product of pair) and add it to the sum
chemistry_sum += skill[i] * skill[-i - 1]
chemistry_sum += skill[i] * skill[n - 1 - i]

return chemistry_sum # Return total chemistry
# Step 4: Return total chemistry
return chemistry_sum