Best Team With No Conflicts - Problem
Basketball Team Formation

You're a basketball team manager preparing for the championship tournament! Your goal is to select players that will give you the highest total score while avoiding team conflicts.

The Challenge: A conflict occurs when a younger player has a strictly higher score than an older player. Players of the same age never conflict with each other.

Input: Two arrays - scores[i] and ages[i] representing each player's skill score and age
Output: The maximum possible total score of a conflict-free team

Example: If you have players with ages [1,2,3] and scores [5,3,6], you can't pick the youngest player (age 1, score 5) with the oldest (age 3, score 6) because the younger player would have a lower score, which is allowed. But you can't pick players with ages [1,3] and scores [5,3] because that would mean the older player has a lower score.

Input & Output

example_1.py — Basic Case
$ Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
› Output: 34
šŸ’” Note: All players are in increasing order of both age and score, so we can pick all players. Total score = 1+3+5+10+15 = 34.
example_2.py — Conflict Resolution
$ Input: scores = [4,5,6,5], ages = [2,1,2,1]
› Output: 16
šŸ’” Note: We can pick players with ages [2,2,1] and scores [4,6,5]. After sorting by age: ages=[1,1,2,2], scores=[5,5,4,6]. The optimal team has score 5+6+5=16.
example_3.py — Edge Case
$ Input: scores = [1,2,3,5], ages = [8,9,10,1]
› Output: 6
šŸ’” Note: The best team is the youngest player (age=1, score=5) and one other player (age=8, score=1), giving total score = 5+1 = 6.

Visualization

Tap to expand
Basketball Team Formation StrategyšŸ‘¶Age: 1Score: 5RookiešŸ§‘Age: 2Score: 3JunioršŸ‘ØAge: 3Score: 6SeniorTeam Formation ProcessāŒ Invalid TeamRookie(5) + Junior(3)Younger has higher score!āœ… Valid TeamRookie(5) + Senior(6)Maintains hierarchy!āœ… Optimal TeamJunior(3) + Senior(6)Score: 9After sorting by age, use DP to find maximum score teamDynamic Programmingdp[i] = max score ending with player iTransition: dp[i] = max(score[i], dp[j] + score[i])for all j where score[j] ≤ score[i]
Understanding the Visualization
1
Sort by Seniority
Arrange players by age to establish clear hierarchy order
2
Build Optimal Teams
Use DP to find the best team ending with each player, ensuring performance consistency
3
Select Best Formation
Choose the team configuration with maximum total performance
Key Takeaway
šŸŽÆ Key Insight: Sorting by age transforms the problem into finding maximum sum increasing subsequence, making conflicts impossible and enabling efficient DP solution.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

O(n log n) for sorting + O(n²) for the DP transitions

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Space for the DP array and sorted pairs

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ scores.length ≤ 1000
  • scores.length == ages.length
  • 1 ≤ scores[i] ≤ 106
  • 1 ≤ ages[i] ≤ 1000
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.3K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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