Skip to content

Commit

Permalink
Merge pull request #882 from 0xff-dev/2491
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2491
  • Loading branch information
6boris committed Jun 6, 2024
2 parents edea25d + 3f01ee1 commit 09c8cb4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# [2491.Divide Players Into Teams of Equal Skill][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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**.

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:**

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

## 题意
> ...
## 题解
**Example 2:**

### 思路1
> ...
Divide Players Into Teams of Equal Skill
```go
```
Input: skill = [3,4]
Output: 12
Explanation:
The two players form a team with a total skill of 7.
The chemistry of the team is 3 * 4 = 12.
```

**Example 3:**

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

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(skill []int) int64 {
sort.Ints(skill)
cur := -1
ans := int64(0)
for s, e := 0, len(skill)-1; s < e; s, e = s+1, e-1 {
if cur == -1 {
cur = skill[s] + skill[e]
ans += int64(skill[s]) * int64(skill[e])
continue
}
if skill[s]+skill[e] != cur {
return -1
}
ans += int64(skill[s]) * int64(skill[e])
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []int
expect int64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{3, 2, 5, 1, 3, 4}, 22},
{"TestCase2", []int{3, 4}, 12},
{"TestCase3", []int{1, 1, 2, 3}, -1},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 09c8cb4

Please sign in to comment.