Best Team With No Conflicts - 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 arrays 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.

Input & Output

Example 1 — Basic Team Selection
$ Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
💡 Note: All players have increasing age and score, so we can select all: 1+3+5+10+15 = 34
Example 2 — Conflict Resolution
$ Input: scores = [4,5,6,5], ages = [2,1,2,1]
Output: 16
💡 Note: Sort by age: [(1,5), (1,5), (2,4), (2,6)]. Best team is players with scores [5,6,5] = 16
Example 3 — Single Player
$ Input: scores = [1], ages = [1]
Output: 1
💡 Note: Only one player available, so team score is 1

Constraints

  • 1 ≤ ages.length ≤ 1000
  • ages.length == scores.length
  • 1 ≤ ages[i] ≤ 1000
  • 1 ≤ scores[i] ≤ 106

Visualization

Tap to expand
Best Team With No Conflicts INPUT scores[]: 1 3 5 10 15 ages[]: 1 2 3 4 5 Players (age, score): (1,1) (2,3) (3,5) (4,10) (5,15) Goal: Max score sum without conflicts Conflict: younger player has higher score ALGORITHM STEPS 1 Sort by Age, then Score Pair and sort players 2 Initialize DP Array dp[i] = score of player i 3 DP Transition If score[j] <= score[i]: dp[i] = max(dp[i], dp[j]+s[i]) 4 Find Maximum Return max(dp[]) DP Array Evolution: 1 4 9 19 34 dp[i] = best sum ending at i FINAL RESULT Best Team Selected: Age: 1 Score: 1 Age: 2 Score: 3 Age: 3 Score: 5 Age: 4 Score: 10 Age: 5 Score: 15 Calculation: 1 + 3 + 5 + 10 + 15 OUTPUT 34 OK - No Conflicts! All 5 players included Scores increase with age Key Insight: Sort players by (age, score) to ensure we only need to check if previous players' scores are less than or equal to current score. This reduces the problem to finding the Longest Increasing Subsequence (LIS) variant where we maximize sum instead of length. TutorialsPoint - Best Team With No Conflicts | Optimized Sort + DP
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
32.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen