Problem

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.

Examples

Example 1:

1
2
3
4
5
Input:
scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output:
 34
Explanation: You can choose all the players.

Example 2:

1
2
3
4
5
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:

1
2
3
4
5
Input:
scores = [1,2,3,5], ages = [8,9,10,1]
Output:
 6
Explanation: It is best to choose the first 3 players.

Solution

Method 1 – Dynamic Programming with Sorting

Intuition

By sorting players by age (and score for tie-breaking), we ensure that when building the team, we only need to check for non-decreasing scores to avoid conflicts. This transforms the problem into finding the maximum sum of a non-decreasing subsequence of scores.

Approach

  1. Pair each player’s age and score.
  2. Sort the pairs by age, then by score.
  3. Use dynamic programming:
  • Let dp[i] be the best team score ending with the i-th player.
  • For each player i, check all previous players j (j < i):
    • If scores[j] <= scores[i], update dp[i] = max(dp[i], dp[j] + scores[i]).
  • The answer is the maximum value in dp.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
   int bestTeamScore(vector<int>& scores, vector<int>& ages) {
      int n = scores.size(), ans = 0;
      vector<pair<int, int>> players(n);
      for (int i = 0; i < n; ++i)
        players[i] = {ages[i], scores[i]};
      sort(players.begin(), players.end());
      vector<int> dp(n, 0);
      for (int i = 0; i < n; ++i) {
        dp[i] = players[i].second;
        for (int j = 0; j < i; ++j) {
           if (players[j].second <= players[i].second)
              dp[i] = max(dp[i], dp[j] + players[i].second);
        }
        ans = max(ans, dp[i]);
      }
      return ans;
   }
};
 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
func bestTeamScore(scores []int, ages []int) int {
   n := len(scores)
   players := make([][2]int, n)
   for i := 0; i < n; i++ {
      players[i] = [2]int{ages[i], scores[i]}
   }
   sort.Slice(players, func(i, j int) bool {
      if players[i][0] == players[j][0] {
        return players[i][1] < players[j][1]
      }
      return players[i][0] < players[j][0]
   })
   dp := make([]int, n)
   ans := 0
   for i := 0; i < n; i++ {
      dp[i] = players[i][1]
      for j := 0; j < i; j++ {
        if players[j][1] <= players[i][1] {
           if dp[j]+players[i][1] > dp[i] {
              dp[i] = dp[j] + players[i][1]
           }
        }
      }
      if dp[i] > ans {
        ans = dp[i]
      }
   }
   return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
   public int bestTeamScore(int[] scores, int[] ages) {
      int n = scores.length, ans = 0;
      int[][] players = new int[n][2];
      for (int i = 0; i < n; i++)
        players[i] = new int[]{ages[i], scores[i]};
      Arrays.sort(players, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
      int[] dp = new int[n];
      for (int i = 0; i < n; i++) {
        dp[i] = players[i][1];
        for (int j = 0; j < i; j++) {
           if (players[j][1] <= players[i][1])
              dp[i] = Math.max(dp[i], dp[j] + players[i][1]);
        }
        ans = Math.max(ans, dp[i]);
      }
      return ans;
   }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
   fun bestTeamScore(scores: IntArray, ages: IntArray): Int {
      val n = scores.size
      val players = Array(n) { i -> ages[i] to scores[i] }
      players.sortWith(compareBy({ it.first }, { it.second }))
      val dp = IntArray(n)
      var ans = 0
      for (i in 0 until n) {
        dp[i] = players[i].second
        for (j in 0 until i) {
           if (players[j].second <= players[i].second) {
              dp[i] = maxOf(dp[i], dp[j] + players[i].second)
           }
        }
        ans = maxOf(ans, dp[i])
      }
      return ans
   }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
   def bestTeamScore(self, scores: list[int], ages: list[int]) -> int:
      players = sorted(zip(ages, scores))
      n = len(scores)
      dp: list[int] = [0] * n
      ans = 0
      for i in range(n):
        dp[i] = players[i][1]
        for j in range(i):
           if players[j][1] <= players[i][1]:
              dp[i] = max(dp[i], dp[j] + players[i][1])
        ans = max(ans, dp[i])
      return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
impl Solution {
   pub fn best_team_score(scores: Vec<i32>, ages: Vec<i32>) -> i32 {
      let n = scores.len();
      let mut players: Vec<(i32, i32)> = ages.into_iter().zip(scores.into_iter()).collect();
      players.sort();
      let mut dp = vec![0; n];
      let mut ans = 0;
      for i in 0..n {
        dp[i] = players[i].1;
        for j in 0..i {
           if players[j].1 <= players[i].1 {
              dp[i] = dp[i].max(dp[j] + players[i].1);
           }
        }
        ans = ans.max(dp[i]);
      }
      ans
   }
}

Complexity

  • ⏰ Time complexity: O(N^2) (due to nested loops after sorting)
  • 🧺 Space complexity: O(N) (for DP array)