Re: [闲聊] 每日leetcode

楼主: dont   2024-10-04 11:11:53
2491. Divide Players Into Teams of Equal Skill
## 思路
chemistry会是最大跟最小的skill相加
排序后, 每次从头尾各取一
如果相加的和不等于chemistry就回传-1
## Code
```python
class Solution:
def dividePlayers(self, skill: List[int]) -> int:
skill.sort()
res = 0
chemistry = skill[0] + skill[-1]
for i in range(len(skill) // 2):
if skill[i] + skill[~i] != chemistry:
return -1
res += skill[i] * skill[~i]
return res
```
作者: DJYOSHITAKA (Evans)   2024-10-04 11:14:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com