Skip to main content
Back to problems
#1626
Medium Algorithms

Best team with no conflicts

Array Dynamic Programming Sorting
50.6% acceptance
Feb 25, 2026
3046
99
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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
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.iter().zip(scores.iter()).map(|(&a, &s)| (a, s)).collect();
    players.sort();
    // dp[i] = max score of team ending with player i
    let mut dp = vec![0i32; n];
    let mut ans = 0;
    for i in 0..n {
      dp[i] = players[i].1;
      for j in 0..i {
        // players[j].age <= players[i].age (sorted)
        // No conflict if scores[j] <= scores[i]
        if players[j].1 <= players[i].1 {
          dp[i] = dp[i].max(dp[j] + players[i].1);
        }
      }
      ans = ans.max(dp[i]);
    }
    ans
  }
}