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 <= 1000scores.length == ages.length1 <= scores[i] <= 1061 <= ages[i] <= 1000Problem Overview: You are given two arrays: scores and ages. Choose a group of players such that no younger player has a strictly higher score than an older player. The goal is to maximize the total team score while respecting this constraint.
Approach 1: Brute Force Backtracking (Exponential Time, O(2^n) time, O(n) space)
Try all possible subsets of players and check whether the team satisfies the conflict rule. For each subset, verify that if player i is younger than player j, then score[i] <= score[j]. If valid, compute the total score and track the maximum. This approach explores every combination using recursion or bitmasking. Time complexity is O(2^n) with O(n) recursion space, which becomes infeasible even for moderate input sizes.
Approach 2: Dynamic Programming with Sorting (O(n^2) time, O(n) space)
The key insight: conflicts only occur when a younger player has a higher score than an older one. Sort players by age, and if ages are equal, sort by score. After sorting, any valid team must have non‑decreasing scores as you move through the list. This turns the problem into a variation of the classic LIS-style dynamic programming problem.
Create a list of (age, score) pairs and sort it. Let dp[i] represent the maximum team score where player i is the last selected player. For each player i, iterate through all previous players j < i. If score[j] <= score[i], player i can join the team ending at j. Update dp[i] = max(dp[i], dp[j] + score[i]). Initialize dp[i] = score[i] to represent a team containing only that player. The answer is the maximum value in dp.
This dynamic programming approach runs in O(n^2) time and uses O(n) space. Sorting removes age conflicts and reduces the constraint to a score ordering problem. The technique combines ideas from sorting, array processing, and dynamic programming.
Recommended for interviews: Interviewers typically expect the sorted dynamic programming solution. Starting with the brute force idea shows you understand the constraints, but recognizing that sorting by age converts the rule into a monotonic score condition demonstrates strong problem‑solving and DP pattern recognition.
First, pair up each player's score and age, and then sort them based on age and score. This allows us to always build teams that do not violate the conflict rule of younger players having higher scores than older players. Subsequently, use dynamic programming to build the optimal solution progressively by calculating the best team score achievable up to this player, considering current player's score only if it resolves no conflicts by adhering to the sorted order.
This solution sorts players by their ages first and scores second to ensure no conflicts. Then it uses dynamic programming to keep track of the best possible score for each player being the last player in the team.
Time Complexity: O(n^2), where n is the number of players.
Space Complexity: O(n) for the DP array.
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Dynamic Programming with Sorting | Time Complexity: O(n^2), where n is the number of players. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Backtracking | O(2^n) | O(n) | Conceptual baseline to understand constraints; impractical for real inputs |
| Dynamic Programming with Sorting | O(n^2) | O(n) | General solution used in interviews; converts the constraint into an LIS-style DP |
Best Team with no Conflicts - Leetcode 1626 - Python • NeetCodeIO • 11,686 views views
Watch 9 more video solutions →Practice Best Team With No Conflicts with our built-in code editor and test cases.
Practice on FleetCode