Description#
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.
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.
Given two lists, scores
and ages
, where each scores[i]
and ages[i]
represents the score and age of the ith
player, respectively, return the highest overall score of all possible basketball teams.
Example 1:
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Explanation: You can choose all the players.
Example 2:
Input: scores = [4,5,6,5], ages = [2,1,2,1]
Output: 16
Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
Example 3:
Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation: It is best to choose the first 3 players.
Constraints:
1 <= scores.length, ages.length <= 1000
scores.length == ages.length
1 <= scores[i] <= 106
1 <= ages[i] <= 1000
Solutions#
Solution 1#
1
2
3
4
5
6
7
8
9
10
11
| class Solution:
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
arr = sorted(zip(scores, ages))
n = len(arr)
f = [0] * n
for i, (score, age) in enumerate(arr):
for j in range(i):
if age >= arr[j][1]:
f[i] = max(f[i], f[j])
f[i] += score
return max(f)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Solution {
public int bestTeamScore(int[] scores, int[] ages) {
int n = ages.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {scores[i], ages[i]};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int[] f = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (arr[i][1] >= arr[j][1]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += arr[i][0];
ans = Math.max(ans, f[i]);
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| class Solution {
public:
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
int n = ages.size();
vector<pair<int, int>> arr(n);
for (int i = 0; i < n; ++i) {
arr[i] = {scores[i], ages[i]};
}
sort(arr.begin(), arr.end());
vector<int> f(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (arr[i].second >= arr[j].second) {
f[i] = max(f[i], f[j]);
}
}
f[i] += arr[i].first;
}
return *max_element(f.begin(), f.end());
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| func bestTeamScore(scores []int, ages []int) int {
n := len(ages)
arr := make([][2]int, n)
for i := range ages {
arr[i] = [2]int{scores[i], ages[i]}
}
sort.Slice(arr, func(i, j int) bool {
a, b := arr[i], arr[j]
return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]
})
f := make([]int, n)
for i := range arr {
for j := 0; j < i; j++ {
if arr[i][1] >= arr[j][1] {
f[i] = max(f[i], f[j])
}
}
f[i] += arr[i][0]
}
return slices.Max(f)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| function bestTeamScore(scores: number[], ages: number[]): number {
const arr = ages.map((age, i) => [age, scores[i]]);
arr.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
const n = arr.length;
const f = new Array(n).fill(0);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (arr[i][1] >= arr[j][1]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += arr[i][1];
}
return Math.max(...f);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| /**
* @param {number[]} scores
* @param {number[]} ages
* @return {number}
*/
var bestTeamScore = function (scores, ages) {
const arr = ages.map((age, i) => [age, scores[i]]);
arr.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
const n = arr.length;
const f = new Array(n).fill(0);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (arr[i][1] >= arr[j][1]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += arr[i][1];
}
return Math.max(...f);
};
|
Solution 2#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| class BinaryIndexedTree:
def __init__(self, n):
self.n = n
self.c = [0] * (n + 1)
def update(self, x, val):
while x <= self.n:
self.c[x] = max(self.c[x], val)
x += x & -x
def query(self, x):
s = 0
while x:
s = max(s, self.c[x])
x -= x & -x
return s
class Solution:
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
m = max(ages)
tree = BinaryIndexedTree(m)
for score, age in sorted(zip(scores, ages)):
tree.update(age, score + tree.query(age))
return tree.query(m)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| class BinaryIndexedTree {
private int n;
private int[] c;
public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
}
public void update(int x, int val) {
while (x <= n) {
c[x] = Math.max(c[x], val);
x += x & -x;
}
}
public int query(int x) {
int s = 0;
while (x > 0) {
s = Math.max(s, c[x]);
x -= x & -x;
}
return s;
}
}
class Solution {
public int bestTeamScore(int[] scores, int[] ages) {
int n = ages.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {scores[i], ages[i]};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int m = 0;
for (int age : ages) {
m = Math.max(m, age);
}
BinaryIndexedTree tree = new BinaryIndexedTree(m);
for (int[] x : arr) {
tree.update(x[1], x[0] + tree.query(x[1]));
}
return tree.query(m);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| class BinaryIndexedTree {
public:
BinaryIndexedTree(int _n)
: n(_n)
, c(_n + 1) {}
void update(int x, int val) {
while (x <= n) {
c[x] = max(c[x], val);
x += x & -x;
}
}
int query(int x) {
int s = 0;
while (x) {
s = max(s, c[x]);
x -= x & -x;
}
return s;
}
private:
int n;
vector<int> c;
};
class Solution {
public:
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
int n = ages.size();
vector<pair<int, int>> arr(n);
for (int i = 0; i < n; ++i) {
arr[i] = {scores[i], ages[i]};
}
sort(arr.begin(), arr.end());
int m = *max_element(ages.begin(), ages.end());
BinaryIndexedTree tree(m);
for (auto& [score, age] : arr) {
tree.update(age, score + tree.query(age));
}
return tree.query(m);
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| type BinaryIndexedTree struct {
n int
c []int
}
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
}
func (this *BinaryIndexedTree) update(x, val int) {
for x <= this.n {
this.c[x] = max(this.c[x], val)
x += x & -x
}
}
func (this *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
s = max(s, this.c[x])
x -= x & -x
}
return s
}
func bestTeamScore(scores []int, ages []int) int {
n := len(ages)
arr := make([][2]int, n)
m := 0
for i, age := range ages {
m = max(m, age)
arr[i] = [2]int{scores[i], age}
}
sort.Slice(arr, func(i, j int) bool {
a, b := arr[i], arr[j]
return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]
})
tree := newBinaryIndexedTree(m)
for _, x := range arr {
tree.update(x[1], x[0]+tree.query(x[1]))
}
return tree.query(m)
}
|