Skip to content

Commit ef5d5cc

Browse files
committedApr 11, 2022
feat: add solutions to lc problem: No.1626
No.1626.Best Team With No Conflicts
1 parent 735fb4c commit ef5d5cc

File tree

9 files changed

+184
-119
lines changed

9 files changed

+184
-119
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- [删列造序 III](/solution/0900-0999/0960.Delete%20Columns%20to%20Make%20Sorted%20III/README.md) - 线性 DP、最长上升子序列模型
7474
- [俄罗斯套娃信封问题](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md) - 线性 DP、最长上升子序列模型、贪心优化
7575
- [堆叠长方体的最大高度](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README.md) - 排序、线性 DP、最长上升子序列模型
76+
- [无矛盾的最佳球队](/solution/1600-1699/1626.Best%20Team%20With%20No%20Conflicts/README.md) - 排序、线性 DP、最长上升子序列模型
7677
<!-- 背包问题、状态机模型、状压DP、区间DP、树形DP、数位DP 待补充 -->
7778

7879
### 4. 高级数据结构

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
7070
- [Delete Columns to Make Sorted III](/solution/0900-0999/0960.Delete%20Columns%20to%20Make%20Sorted%20III/README_EN.md) - Linear problem, LIS
7171
- [Russian Doll Envelopes](/solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md) - Linear problem, LIS
7272
- [Maximum Height by Stacking Cuboids](/solution/1600-1699/1691.Maximum%20Height%20by%20Stacking%20Cuboids/README_EN.md) - Sort, Linear problem, LIS
73+
- [Best Team With No Conflicts](/solution/1600-1699/1626.Best%20Team%20With%20No%20Conflicts/README_EN.md) - Sort, Linear problem, LIS
7374

7475
### 4. Advanced Data Structures
7576

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

+65-41
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
动态规划 - 最长上升子序列模型。
52+
**方法一:动态规划**
5353

54-
将所有球员先按照年龄从小到大排序(年龄相同,则按照分数从小到大排),然后在分数数组中求解最长上升子序列和的最大值即可。
54+
最长上升子序列模型。
55+
56+
将所有球员先按照年龄从小到大排序(年龄相同,则按照分数从小到大排),然后在分数数组中求解最长上升子序列和的最大值即可。最长上升子序列朴素做法,时间复杂度 O(n²)。
5557

5658
类似题型:洛谷 “[P2782 友好城市](https://www.luogu.com.cn/problem/P2782)”。
5759

@@ -64,16 +66,15 @@
6466
```python
6567
class Solution:
6668
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-
dp = [num[0] for num in nums]
70-
res, n = 0, len(ages)
69+
nums = list(zip(ages, scores))
70+
nums.sort()
71+
n = len(nums)
72+
dp = [num[1] for num in nums]
7173
for i in range(n):
7274
for j in range(i):
73-
if nums[j][0] <= nums[i][0]:
74-
dp[i] = max(dp[i], dp[j] + nums[i][0])
75-
res = max(res, dp[i])
76-
return res
75+
if nums[i][1] >= nums[j][1]:
76+
dp[i] = max(dp[i], dp[j] + nums[i][1])
77+
return max(dp)
7778
```
7879

7980
### **Java**
@@ -86,23 +87,23 @@ class Solution {
8687
int n = ages.length;
8788
int[][] nums = new int[n][2];
8889
for (int i = 0; i < n; ++i) {
89-
nums[i] = new int[]{scores[i], ages[i]};
90+
nums[i] = new int[]{ages[i], scores[i]};
9091
}
9192
Arrays.sort(nums, (a, b) -> {
92-
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
93+
return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
9394
});
9495
int[] dp = new int[n];
95-
int res = 0;
96+
int ans = 0;
9697
for (int i = 0; i < n; ++i) {
97-
dp[i] = nums[i][0];
98+
dp[i] = nums[i][1];
9899
for (int j = 0; j < i; ++j) {
99-
if (nums[j][0] <= nums[i][0]) {
100-
dp[i] = Math.max(dp[i], dp[j] + nums[i][0]);
100+
if (nums[i][1] >= nums[j][1]) {
101+
dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
101102
}
102103
}
103-
res = Math.max(res, dp[i]);
104+
ans = Math.max(ans, dp[i]);
104105
}
105-
return res;
106+
return ans;
106107
}
107108
}
108109
```
@@ -112,26 +113,22 @@ class Solution {
112113
```cpp
113114
class Solution {
114115
public:
115-
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
116-
vector<pair<int, int>> nums;
116+
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
117117
int n = ages.size();
118-
for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]});
119-
sort(nums.begin(), nums.end(), [](auto &a, auto &b) {
120-
return a.second == b.second ? a.first < b.first : a.second < b.second;
121-
});
118+
vector<vector<int>> nums(n);
119+
for (int i = 0; i < n; ++i) nums[i] = {ages[i], scores[i]};
120+
sort(nums.begin(), nums.end());
122121
vector<int> dp(n);
123-
int res = 0;
124122
for (int i = 0; i < n; ++i)
125123
{
126-
dp[i] = nums[i].first;
124+
dp[i] = nums[i][1];
127125
for (int j = 0; j < i; ++j)
128126
{
129-
if (nums[j].first <= nums[i].first)
130-
dp[i] = max(dp[i], dp[j] + nums[i].first);
127+
if (nums[i][1] >= nums[j][1])
128+
dp[i] = max(dp[i], dp[j] + nums[i][1]);
131129
}
132-
res = max(res, dp[i]);
133130
}
134-
return res;
131+
return *max_element(dp.begin(), dp.end());
135132
}
136133
};
137134
```
@@ -141,28 +138,28 @@ public:
141138
```go
142139
func bestTeamScore(scores []int, ages []int) int {
143140
n := len(ages)
144-
var nums [][]int
145-
for i := 0; i < n; i++ {
146-
nums = append(nums, []int{scores[i], ages[i]})
141+
nums := make([][]int, n)
142+
for i, age := range ages {
143+
nums[i] = []int{age, scores[i]}
147144
}
148145
sort.Slice(nums, func(i, j int) bool {
149-
if nums[i][1] == nums[j][1] {
146+
if nums[i][0] != nums[j][0] {
150147
return nums[i][0] < nums[j][0]
151148
}
152149
return nums[i][1] < nums[j][1]
153150
})
154151
dp := make([]int, n)
155-
res := 0
156-
for i := 0; i < n; i++ {
157-
dp[i] = nums[i][0]
152+
ans := 0
153+
for i, num := range nums {
154+
dp[i] = num[1]
158155
for j := 0; j < i; j++ {
159-
if nums[j][0] <= nums[i][0] {
160-
dp[i] = max(dp[i], dp[j]+nums[i][0])
156+
if num[1] >= nums[j][1] {
157+
dp[i] = max(dp[i], dp[j]+num[1])
161158
}
162159
}
163-
res = max(res, dp[i])
160+
ans = max(ans, dp[i])
164161
}
165-
return res
162+
return ans
166163
}
167164
168165
func max(a, b int) int {
@@ -173,6 +170,33 @@ func max(a, b int) int {
173170
}
174171
```
175172

173+
### **JavaScript**
174+
175+
```js
176+
/**
177+
* @param {number[]} scores
178+
* @param {number[]} ages
179+
* @return {number}
180+
*/
181+
var bestTeamScore = function (scores, ages) {
182+
const nums = ages.map((age, i) => [age, scores[i]]);
183+
nums.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
184+
const n = nums.length;
185+
let dp = new Array(n);
186+
let ans = 0;
187+
for (let i = 0; i < n; ++i) {
188+
dp[i] = nums[i][1];
189+
for (let j = 0; j < i; ++j) {
190+
if (nums[i][1] >= nums[j][1]) {
191+
dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
192+
}
193+
}
194+
ans = Math.max(ans, dp[i]);
195+
}
196+
return ans;
197+
};
198+
```
199+
176200
### **...**
177201

178202
```

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

+61-39
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ LIS.
5656
```python
5757
class Solution:
5858
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-
dp = [num[0] for num in nums]
62-
res, n = 0, len(ages)
59+
nums = list(zip(ages, scores))
60+
nums.sort()
61+
n = len(nums)
62+
dp = [num[1] for num in nums]
6363
for i in range(n):
6464
for j in range(i):
65-
if nums[j][0] <= nums[i][0]:
66-
dp[i] = max(dp[i], dp[j] + nums[i][0])
67-
res = max(res, dp[i])
68-
return res
65+
if nums[i][1] >= nums[j][1]:
66+
dp[i] = max(dp[i], dp[j] + nums[i][1])
67+
return max(dp)
6968
```
7069

7170
### **Java**
@@ -76,23 +75,23 @@ class Solution {
7675
int n = ages.length;
7776
int[][] nums = new int[n][2];
7877
for (int i = 0; i < n; ++i) {
79-
nums[i] = new int[]{scores[i], ages[i]};
78+
nums[i] = new int[]{ages[i], scores[i]};
8079
}
8180
Arrays.sort(nums, (a, b) -> {
82-
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
81+
return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0];
8382
});
8483
int[] dp = new int[n];
85-
int res = 0;
84+
int ans = 0;
8685
for (int i = 0; i < n; ++i) {
87-
dp[i] = nums[i][0];
86+
dp[i] = nums[i][1];
8887
for (int j = 0; j < i; ++j) {
89-
if (nums[j][0] <= nums[i][0]) {
90-
dp[i] = Math.max(dp[i], dp[j] + nums[i][0]);
88+
if (nums[i][1] >= nums[j][1]) {
89+
dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
9190
}
9291
}
93-
res = Math.max(res, dp[i]);
92+
ans = Math.max(ans, dp[i]);
9493
}
95-
return res;
94+
return ans;
9695
}
9796
}
9897
```
@@ -102,26 +101,22 @@ class Solution {
102101
```cpp
103102
class Solution {
104103
public:
105-
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
106-
vector<pair<int, int>> nums;
104+
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
107105
int n = ages.size();
108-
for (int i = 0; i < n; ++i) nums.push_back({scores[i], ages[i]});
109-
sort(nums.begin(), nums.end(), [](auto &a, auto &b) {
110-
return a.second == b.second ? a.first < b.first : a.second < b.second;
111-
});
106+
vector<vector<int>> nums(n);
107+
for (int i = 0; i < n; ++i) nums[i] = {ages[i], scores[i]};
108+
sort(nums.begin(), nums.end());
112109
vector<int> dp(n);
113-
int res = 0;
114110
for (int i = 0; i < n; ++i)
115111
{
116-
dp[i] = nums[i].first;
112+
dp[i] = nums[i][1];
117113
for (int j = 0; j < i; ++j)
118114
{
119-
if (nums[j].first <= nums[i].first)
120-
dp[i] = max(dp[i], dp[j] + nums[i].first);
115+
if (nums[i][1] >= nums[j][1])
116+
dp[i] = max(dp[i], dp[j] + nums[i][1]);
121117
}
122-
res = max(res, dp[i]);
123118
}
124-
return res;
119+
return *max_element(dp.begin(), dp.end());
125120
}
126121
};
127122
```
@@ -131,28 +126,28 @@ public:
131126
```go
132127
func bestTeamScore(scores []int, ages []int) int {
133128
n := len(ages)
134-
var nums [][]int
135-
for i := 0; i < n; i++ {
136-
nums = append(nums, []int{scores[i], ages[i]})
129+
nums := make([][]int, n)
130+
for i, age := range ages {
131+
nums[i] = []int{age, scores[i]}
137132
}
138133
sort.Slice(nums, func(i, j int) bool {
139-
if nums[i][1] == nums[j][1] {
134+
if nums[i][0] != nums[j][0] {
140135
return nums[i][0] < nums[j][0]
141136
}
142137
return nums[i][1] < nums[j][1]
143138
})
144139
dp := make([]int, n)
145-
res := 0
146-
for i := 0; i < n; i++ {
147-
dp[i] = nums[i][0]
140+
ans := 0
141+
for i, num := range nums {
142+
dp[i] = num[1]
148143
for j := 0; j < i; j++ {
149-
if nums[j][0] <= nums[i][0] {
150-
dp[i] = max(dp[i], dp[j]+nums[i][0])
144+
if num[1] >= nums[j][1] {
145+
dp[i] = max(dp[i], dp[j]+num[1])
151146
}
152147
}
153-
res = max(res, dp[i])
148+
ans = max(ans, dp[i])
154149
}
155-
return res
150+
return ans
156151
}
157152
158153
func max(a, b int) int {
@@ -163,6 +158,33 @@ func max(a, b int) int {
163158
}
164159
```
165160

161+
### **JavaScript**
162+
163+
```js
164+
/**
165+
* @param {number[]} scores
166+
* @param {number[]} ages
167+
* @return {number}
168+
*/
169+
var bestTeamScore = function (scores, ages) {
170+
const nums = ages.map((age, i) => [age, scores[i]]);
171+
nums.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
172+
const n = nums.length;
173+
let dp = new Array(n);
174+
let ans = 0;
175+
for (let i = 0; i < n; ++i) {
176+
dp[i] = nums[i][1];
177+
for (let j = 0; j < i; ++j) {
178+
if (nums[i][1] >= nums[j][1]) {
179+
dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
180+
}
181+
}
182+
ans = Math.max(ans, dp[i]);
183+
}
184+
return ans;
185+
};
186+
```
187+
166188
### **...**
167189

168190
```
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
class Solution {
22
public:
3-
int bestTeamScore(vector<int> &scores, vector<int> &ages) {
4-
vector<pair<int, int>> nums;
3+
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
54
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-
});
5+
vector<vector<int>> nums(n);
6+
for (int i = 0; i < n; ++i) nums[i] = {ages[i], scores[i]};
7+
sort(nums.begin(), nums.end());
108
vector<int> dp(n);
11-
int res = 0;
129
for (int i = 0; i < n; ++i)
1310
{
14-
dp[i] = nums[i].first;
11+
dp[i] = nums[i][1];
1512
for (int j = 0; j < i; ++j)
1613
{
17-
if (nums[j].first <= nums[i].first)
18-
dp[i] = max(dp[i], dp[j] + nums[i].first);
14+
if (nums[i][1] >= nums[j][1])
15+
dp[i] = max(dp[i], dp[j] + nums[i][1]);
1916
}
20-
res = max(res, dp[i]);
2117
}
22-
return res;
18+
return *max_element(dp.begin(), dp.end());
2319
}
2420
};

0 commit comments

Comments
 (0)
Please sign in to comment.