diff --git a/Python/Divide_Players_Into_Teams_Of_Equal_Skills.py b/Python/Divide_Players_Into_Teams_Of_Equal_Skills.py index e483c1a..0c1f10a 100644 --- a/Python/Divide_Players_Into_Teams_Of_Equal_Skills.py +++ b/Python/Divide_Players_Into_Teams_Of_Equal_Skills.py @@ -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 \ No newline at end of file + # Step 4: Return total chemistry + return chemistry_sum