Skip to content

Commit 09c8cb4

Browse files
authored
Merge pull request #882 from 0xff-dev/2491
Add solution and test-cases for problem 2491
2 parents edea25d + 3f01ee1 commit 09c8cb4

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [2491.Divide Players Into Teams of Equal Skill][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a positive integer array `skill` of **even** length `n` where `skill[i]` denotes the skill of the i<sup>th</sup>. player. Divide the players into `n / 2` teams of size `2` such that the total skill of each team is **equal**.
5+
6+
The **chemistry** of a team is equal to the **product** of the skills of the players on that team.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: skill = [3,2,5,1,3,4]
14+
Output: 22
15+
Explanation:
16+
Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
17+
The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Divide Players Into Teams of Equal Skill
23-
```go
2422
```
23+
Input: skill = [3,4]
24+
Output: 12
25+
Explanation:
26+
The two players form a team with a total skill of 7.
27+
The chemistry of the team is 3 * 4 = 12.
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: skill = [1,1,2,3]
34+
Output: -1
35+
Explanation:
36+
There is no way to divide the players into teams such that the total skill of each team is equal.
37+
```
2638

2739
## 结语
2840

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(skill []int) int64 {
6+
sort.Ints(skill)
7+
cur := -1
8+
ans := int64(0)
9+
for s, e := 0, len(skill)-1; s < e; s, e = s+1, e-1 {
10+
if cur == -1 {
11+
cur = skill[s] + skill[e]
12+
ans += int64(skill[s]) * int64(skill[e])
13+
continue
14+
}
15+
if skill[s]+skill[e] != cur {
16+
return -1
17+
}
18+
ans += int64(skill[s]) * int64(skill[e])
19+
}
20+
return ans
521
}

leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int64
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 2, 5, 1, 3, 4}, 22},
17+
{"TestCase2", []int{3, 4}, 12},
18+
{"TestCase3", []int{1, 1, 2, 3}, -1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)