Skip to content

Commit 40f0c40

Browse files
committed
Add solution and test-cases for problem 1626
1 parent 9ef75d2 commit 40f0c40

File tree

3 files changed

+66
-26
lines changed

3 files changed

+66
-26
lines changed

leetcode/1601-1700/1626.Best-Team-With-No-Conflicts/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [1626.Best Team With No Conflicts][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 the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the **sum** of scores of all the players in the team.
5+
6+
However, the basketball team is not allowed to have **conflicts**. A **conflict** exists if a younger player has a **strictly higher** score than an older player. A conflict does **not** occur between players of the same age.
7+
8+
Given two lists, `scores` and `ages`, where each `scores[i]` and `ages[i]` represents the score and age of the i<sup>th</sup> player, respectively, return the highest overall score of all possible basketball teams.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
14+
Output: 34
15+
Explanation: You can choose all the players.
1316
```
1417

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

20-
### 思路1
21-
> ...
22-
Best Team With No Conflicts
23-
```go
2420
```
21+
Input: scores = [4,5,6,5], ages = [2,1,2,1]
22+
Output: 16
23+
Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
24+
```
25+
26+
**Example 3:**
2527

28+
```
29+
Input: scores = [1,2,3,5], ages = [8,9,10,1]
30+
Output: 6
31+
Explanation: It is best to choose the first 3 players.
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func sort1626(scores, ages []int) {
4+
l := len(scores)
5+
for i := 0; i < l-1; i++ {
6+
for j := 0; j < l-1-i; j++ {
7+
if (ages[j] > ages[j+1]) || (ages[j] == ages[j+1] && scores[j] > scores[j+1]) {
8+
ages[j], ages[j+1] = ages[j+1], ages[j]
9+
scores[j], scores[j+1] = scores[j+1], scores[j]
10+
}
11+
}
12+
}
13+
}
14+
func Solution(scores []int, ages []int) int {
15+
sort1626(scores, ages)
16+
l := len(scores)
17+
dp := make([]int, l)
18+
ans := 0
19+
for i := 0; i < l; i++ {
20+
dp[i] = scores[i]
21+
if dp[i] > ans {
22+
ans = dp[i]
23+
}
24+
}
25+
for i := 1; i < l; i++ {
26+
for pre := i - 1; pre >= 0; pre-- {
27+
if scores[i] >= scores[pre] {
28+
if r := dp[pre] + scores[i]; r > dp[i] {
29+
dp[i] = r
30+
}
31+
}
32+
}
33+
if dp[i] > ans {
34+
ans = dp[i]
35+
}
36+
}
37+
return ans
538
}

leetcode/1601-1700/1626.Best-Team-With-No-Conflicts/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.scores, c.ages)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.scores, c.ages)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)