Program to find best team with no conflicts in Python

Suppose we have two lists called scores and ages, where scores[i] and ages[i] represent the score and age of the ith player in a basketball game. We want to select the team with the highest overall score, where the team score is the total sum of scores of all selected players. However, we must avoid conflicts − a conflict exists if a younger player has a strictly higher score than an older player.

So, if the input is like scores = [5,7,9,14,19], ages = [5,6,7,8,9], then the output will be 54 as we can select all players without any conflicts.

Algorithm

To solve this problem, we will use dynamic programming with the following approach:

  • Create pairs of (age, score) and sort them by age
  • Use dynamic programming where dp[i] represents the maximum score achievable ending with player i
  • For each player, consider adding them to all valid previous combinations
  • A combination is valid if the previous player's score ? current player's score (no conflict)

Implementation

def solve(scores, ages):
    # Create pairs of (age, score) and sort by age
    age_score_pairs = [[a, s] for a, s in zip(ages, scores)]
    age_score_pairs.sort()
    
    # Extract scores in age-sorted order
    sorted_scores = [s for a, s in age_score_pairs]
    
    max_score = 0
    n = len(sorted_scores)
    dp = [0] * n
    
    for i in range(n):
        current_score = sorted_scores[i]
        dp[i] = current_score  # Player alone
        
        # Try adding current player to previous valid combinations
        for j in range(i):
            if sorted_scores[j] <= current_score:
                dp[i] = max(dp[i], dp[j] + current_score)
        
        max_score = max(max_score, dp[i])
    
    return max_score

# Test the function
scores = [5, 7, 9, 14, 19]
ages = [5, 6, 7, 8, 9]
result = solve(scores, ages)
print(f"Maximum team score: {result}")
Maximum team score: 54

How It Works

The algorithm works by:

  1. Sorting by age: This ensures we process players from youngest to oldest
  2. Dynamic programming: For each player, we calculate the maximum score achievable if that player is included in the team
  3. Conflict checking: We only add a player to a previous combination if their score ? the previous player's score
  4. Optimization: We keep track of the maximum score seen so far

Example Walkthrough

def solve_with_trace(scores, ages):
    age_score_pairs = [[a, s] for a, s in zip(ages, scores)]
    age_score_pairs.sort()
    sorted_scores = [s for a, s in age_score_pairs]
    
    print(f"Age-score pairs (sorted): {age_score_pairs}")
    print(f"Scores in age order: {sorted_scores}")
    
    n = len(sorted_scores)
    dp = [0] * n
    
    for i in range(n):
        current_score = sorted_scores[i]
        dp[i] = current_score
        
        for j in range(i):
            if sorted_scores[j] <= current_score:
                dp[i] = max(dp[i], dp[j] + current_score)
        
        print(f"dp[{i}] = {dp[i]} (player with score {current_score})")
    
    return max(dp)

# Example with trace
scores = [5, 7, 9, 14, 19]
ages = [5, 6, 7, 8, 9]
result = solve_with_trace(scores, ages)
print(f"Final result: {result}")
Age-score pairs (sorted): [[5, 5], [6, 7], [7, 9], [8, 14], [9, 19]]
Scores in age order: [5, 7, 9, 14, 19]
dp[0] = 5 (player with score 5)
dp[1] = 12 (player with score 7)
dp[2] = 21 (player with score 9)
dp[3] = 35 (player with score 14)
dp[4] = 54 (player with score 19)
Final result: 54

Time and Space Complexity

  • Time Complexity: O(n²) where n is the number of players
  • Space Complexity: O(n) for the DP array and sorted pairs

Conclusion

This dynamic programming solution efficiently finds the maximum team score while avoiding conflicts. By sorting players by age first, we ensure that any valid team combination maintains the age-score constraint, making it a classic longest increasing subsequence variant.

Updated on: 2026-03-26T13:51:38+05:30

581 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements