2491. Divide Players Into Teams of Equal Skill
Medium
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:
Example 2:
Example 3:
Constraints:
2 <= skill.length <= 10^5
skill.length
is even.1 <= skill[i] <= 1000
解題
將陣列分成兩兩一組,每組加起來的值必須相同。
如果符合條件的話,從小排到大,最小+最大 = 次小 + 次大......以此類推,不符合就回傳 -1
Runtime: 82 ms, faster than 90.27%
Memory Usage: 8.4 MB, less than 80.53%
Last updated