diff --git a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/README.md b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/README.md index 0f5f2e43f..5dd3f4205 100755 --- a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/README.md +++ b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/README.md @@ -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 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:** ``` -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. +``` ## 结语 diff --git a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution.go b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution.go index d115ccf5e..ebe45e34f 100644 --- a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution.go +++ b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution.go @@ -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 } diff --git a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution_test.go b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution_test.go index 14ff50eb4..5c922e50b 100644 --- a/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution_test.go +++ b/leetcode/2401-2500/2491.Divide-Players-Into-Teams-of-Equal-Skill/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }