Skip to content

Commit 4d59c31

Browse files
committed
feat: add solutions to lc problem: No.1626.Best Team With No Conflicts
1 parent 0280ee0 commit 4d59c31

File tree

6 files changed

+305
-4
lines changed

6 files changed

+305
-4
lines changed

solution/1600-1699/1626.Best Team With No Conflicts/README.md

+108-2
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,133 @@
4545
<li><code>1 &lt;= ages[i] &lt;= 1000</code></li>
4646
</ul>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
5251

52+
动态规划 - 最长上升子序列模型。
53+
54+
将所有球员先按照年龄从小到大排序(年龄相同,则按照分数从小到大排),然后在分数数组中求解最长上升子序列和的最大值即可。
55+
56+
类似题型:洛谷 “[P2782 友好城市](https://www.luogu.com.cn/problem/P2782)”。
57+
5358
<!-- tabs:start -->
5459

5560
### **Python3**
5661

5762
<!-- 这里可写当前语言的特殊实现逻辑 -->
5863

5964
```python
60-
65+
class Solution:
66+
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
67+
nums = list(zip(scores, ages))
68+
nums.sort(key=lambda x: (x[1], x[0]))
69+
n = len(ages)
70+
dp = [nums[i][0] for i in range(n)]
71+
res = 0
72+
for i in range(n):
73+
for j in range(i):
74+
if nums[j][0] <= nums[i][0]:
75+
dp[i] = max(dp[i], dp[j] + nums[i][0])
76+
res = max(res, dp[i])
77+
return res
6178
```
6279

6380
### **Java**
6481

6582
<!-- 这里可写当前语言的特殊实现逻辑 -->
6683

6784
```java
85+
class Solution {
86+
public int bestTeamScore(int[] scores, int[] ages) {
87+
int n = ages.length;
88+
int[][] nums = new int[n][2];
89+
for (int i = 0; i < n; ++i) {
90+
nums[i] = new int[]{scores[i], ages[i]};
91+
}
92+
Arrays.sort(nums, (a, b) -> {
93+
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
94+
});
95+
int[] dp = new int[n];
96+
int res = 0;
97+
for (int i = 0; i < n; ++i) {
98+
dp[i] = nums[i][0];
99+
for (int j = 0; j < i; ++j) {
100+
if (nums[j][0] <= nums[i][0]) {
101+
dp[i] = Math.max(dp[i], dp[j] + nums[i][0]);
102+
}
103+
}
104+
res = Math.max(res, dp[i]);
105+
}
106+
return res;
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
117+
vector<pair<int, int>> nums;
118+
int n = ages.size();
119+
for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]});
120+
sort(nums.begin(), nums.end(), [](auto &a, auto &b) {
121+
return a.second == b.second ? a.first < b.first : a.second < b.second;
122+
});
123+
vector<int> dp(n);
124+
int res = 0;
125+
for (int i = 0; i < n; ++i)
126+
{
127+
dp[i] = nums[i].first;
128+
for (int j = 0; j < i; ++j)
129+
{
130+
if (nums[j].first <= nums[i].first)
131+
dp[i] = max(dp[i], dp[j] + nums[i].first);
132+
}
133+
res = max(res, dp[i]);
134+
}
135+
return res;
136+
}
137+
};
138+
```
68139
140+
### **Go**
141+
142+
```go
143+
func bestTeamScore(scores []int, ages []int) int {
144+
n := len(ages)
145+
var nums [][]int
146+
for i := 0; i < n; i++ {
147+
nums = append(nums, []int{scores[i], ages[i]})
148+
}
149+
sort.Slice(nums, func(i, j int) bool {
150+
if nums[i][1] == nums[j][1] {
151+
return nums[i][0] < nums[j][0]
152+
}
153+
return nums[i][1] < nums[j][1]
154+
})
155+
dp := make([]int, n)
156+
res := 0
157+
for i := 0; i < n; i++ {
158+
dp[i] = nums[i][0]
159+
for j := 0; j < i; j++ {
160+
if nums[j][0] <= nums[i][0] {
161+
dp[i] = max(dp[i], dp[j]+nums[i][0])
162+
}
163+
}
164+
res = max(res, dp[i])
165+
}
166+
return res
167+
}
168+
169+
func max(a, b int) int {
170+
if a > b {
171+
return a
172+
}
173+
return b
174+
}
69175
```
70176

71177
### **...**

solution/1600-1699/1626.Best Team With No Conflicts/README_EN.md

+104-2
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,123 @@
4545
<li><code>1 &lt;= ages[i] &lt;= 1000</code></li>
4646
</ul>
4747

48-
4948
## Solutions
5049

50+
LIS.
51+
5152
<!-- tabs:start -->
5253

5354
### **Python3**
5455

5556
```python
56-
57+
class Solution:
58+
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
59+
nums = list(zip(scores, ages))
60+
nums.sort(key=lambda x: (x[1], x[0]))
61+
n = len(ages)
62+
dp = [nums[i][0] for i in range(n)]
63+
res = 0
64+
for i in range(n):
65+
for j in range(i):
66+
if nums[j][0] <= nums[i][0]:
67+
dp[i] = max(dp[i], dp[j] + nums[i][0])
68+
res = max(res, dp[i])
69+
return res
5770
```
5871

5972
### **Java**
6073

6174
```java
75+
class Solution {
76+
public int bestTeamScore(int[] scores, int[] ages) {
77+
int n = ages.length;
78+
int[][] nums = new int[n][2];
79+
for (int i = 0; i < n; ++i) {
80+
nums[i] = new int[]{scores[i], ages[i]};
81+
}
82+
Arrays.sort(nums, (a, b) -> {
83+
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
84+
});
85+
int[] dp = new int[n];
86+
int res = 0;
87+
for (int i = 0; i < n; ++i) {
88+
dp[i] = nums[i][0];
89+
for (int j = 0; j < i; ++j) {
90+
if (nums[j][0] <= nums[i][0]) {
91+
dp[i] = Math.max(dp[i], dp[j] + nums[i][0]);
92+
}
93+
}
94+
res = Math.max(res, dp[i]);
95+
}
96+
return res;
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
107+
vector<pair<int, int>> nums;
108+
int n = ages.size();
109+
for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]});
110+
sort(nums.begin(), nums.end(), [](auto &a, auto &b) {
111+
return a.second == b.second ? a.first < b.first : a.second < b.second;
112+
});
113+
vector<int> dp(n);
114+
int res = 0;
115+
for (int i = 0; i < n; ++i)
116+
{
117+
dp[i] = nums[i].first;
118+
for (int j = 0; j < i; ++j)
119+
{
120+
if (nums[j].first <= nums[i].first)
121+
dp[i] = max(dp[i], dp[j] + nums[i].first);
122+
}
123+
res = max(res, dp[i]);
124+
}
125+
return res;
126+
}
127+
};
128+
```
62129
130+
### **Go**
131+
132+
```go
133+
func bestTeamScore(scores []int, ages []int) int {
134+
n := len(ages)
135+
var nums [][]int
136+
for i := 0; i < n; i++ {
137+
nums = append(nums, []int{scores[i], ages[i]})
138+
}
139+
sort.Slice(nums, func(i, j int) bool {
140+
if nums[i][1] == nums[j][1] {
141+
return nums[i][0] < nums[j][0]
142+
}
143+
return nums[i][1] < nums[j][1]
144+
})
145+
dp := make([]int, n)
146+
res := 0
147+
for i := 0; i < n; i++ {
148+
dp[i] = nums[i][0]
149+
for j := 0; j < i; j++ {
150+
if nums[j][0] <= nums[i][0] {
151+
dp[i] = max(dp[i], dp[j]+nums[i][0])
152+
}
153+
}
154+
res = max(res, dp[i])
155+
}
156+
return res
157+
}
158+
159+
func max(a, b int) int {
160+
if a > b {
161+
return a
162+
}
163+
return b
164+
}
63165
```
64166

65167
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
4+
vector<pair<int, int>> nums;
5+
int n = ages.size();
6+
for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]});
7+
sort(nums.begin(), nums.end(), [](auto &a, auto &b) {
8+
return a.second == b.second ? a.first < b.first : a.second < b.second;
9+
});
10+
vector<int> dp(n);
11+
int res = 0;
12+
for (int i = 0; i < n; ++i)
13+
{
14+
dp[i] = nums[i].first;
15+
for (int j = 0; j < i; ++j)
16+
{
17+
if (nums[j].first <= nums[i].first)
18+
dp[i] = max(dp[i], dp[j] + nums[i].first);
19+
}
20+
res = max(res, dp[i]);
21+
}
22+
return res;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func bestTeamScore(scores []int, ages []int) int {
2+
n := len(ages)
3+
var nums [][]int
4+
for i := 0; i < n; i++ {
5+
nums = append(nums, []int{scores[i], ages[i]})
6+
}
7+
sort.Slice(nums, func(i, j int) bool {
8+
if nums[i][1] == nums[j][1] {
9+
return nums[i][0] < nums[j][0]
10+
}
11+
return nums[i][1] < nums[j][1]
12+
})
13+
dp := make([]int, n)
14+
res := 0
15+
for i := 0; i < n; i++ {
16+
dp[i] = nums[i][0]
17+
for j := 0; j < i; j++ {
18+
if nums[j][0] <= nums[i][0] {
19+
dp[i] = max(dp[i], dp[j]+nums[i][0])
20+
}
21+
}
22+
res = max(res, dp[i])
23+
}
24+
return res
25+
}
26+
27+
func max(a, b int) int {
28+
if a > b {
29+
return a
30+
}
31+
return b
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int bestTeamScore(int[] scores, int[] ages) {
3+
int n = ages.length;
4+
int[][] nums = new int[n][2];
5+
for (int i = 0; i < n; ++i) {
6+
nums[i] = new int[]{scores[i], ages[i]};
7+
}
8+
Arrays.sort(nums, (a, b) -> {
9+
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
10+
});
11+
int[] dp = new int[n];
12+
int res = 0;
13+
for (int i = 0; i < n; ++i) {
14+
dp[i] = nums[i][0];
15+
for (int j = 0; j < i; ++j) {
16+
if (nums[j][0] <= nums[i][0]) {
17+
dp[i] = Math.max(dp[i], dp[j] + nums[i][0]);
18+
}
19+
}
20+
res = Math.max(res, dp[i]);
21+
}
22+
return res;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
3+
nums = list(zip(scores, ages))
4+
nums.sort(key=lambda x: (x[1], x[0]))
5+
n = len(ages)
6+
dp = [nums[i][0] for i in range(n)]
7+
res = 0
8+
for i in range(n):
9+
for j in range(i):
10+
if nums[j][0] <= nums[i][0]:
11+
dp[i] = max(dp[i], dp[j] + nums[i][0])
12+
res = max(res, dp[i])
13+
return res

0 commit comments

Comments
 (0)