Leetcode 1626. Best Team With No Conflicts

Problem Explanation:

In this problem, we are the manager of a basketball team and we want to form a team for an upcoming tournament. We want the team with the highest overall score, where the score of the team is the sum of scores of all the players in the team. However, there is a constraint - the team can't have any conflicts, meaning a younger player can't have a higher score than an older player. No conflicts occur between players of the same age. Our task is to return the highest overall score of all possible basketball teams given two lists where each scores[i] and ages[i] represents the score and age of the ith player respectively.

For example, let scores = [4,5,6,5] and ages = [2,1,2,1]. Here, the best team to choose is the last 3 players which will give the highest overall score of 16, as conflict arises if we add the 1st player.

Solution Approach:

This problem can be solved using dynamic programming and sorting. The idea is to sort the players based on age in increasing order. If two players have the same age, we break ties on score.

For each player, we calculate the maximum possible score, assuming that this player is on the team. We use dynamic programming for this calculation, where dp[i] is the maximum score you can get if you have to take player i.

While populating dp[i], we check all j < i. If players[j] has score greater or equal to players[i], this means we can select both player i and player j without any conflict, since player j is older. We try adding player i's score to the maximum score including player j ( dp[j] + players[i]) and compare it to the maximum score without player i ( dp[i] ), and take the maximum.

At the end, the maximum value in dp array is the maximum possible score a team can get without conflicts.

C++ Solution:

3struct Player {
4  int age, score;
5  Player(int age, int score) : age(age), score(score) {}
8class Solution {
9 public:
10  int bestTeamScore(vector<int>& scores, vector<int>& ages) {
11    vector<Player> players;
12    for (int i = 0; i < scores.size(); i++)
13      players.emplace_back(ages[i], scores[i]);
15    sort(players.begin(), players.end(), [](const Player &a, const Player &b) {
16      if (a.age != b.age)
17        return a.age < b.age;
18      return a.score < b.score;
19    });
21    vector<int> dp(scores.size(), 0);
22    for (int i = 0; i < players.size(); i++) {
23      dp[i] = players[i].score;
24      for (int j = 0; j < i; j++)
25        if (players[j].score <= players[i].score)
26          dp[i] = max(dp[i], dp[j] + players[i].score);
27    }
28    return *max_element(dp.begin(), dp.end());
29  }

In this C++ solution, we first create a Player class to hold the ages and scores of the players. We then sort the players in ascending order of ages using a custom comparator. After the sorting, we use a dynamic programming array to find the maximum score. Each element dp[i] of the DP array holds the best score we can get taking player at position i in our team. Finally, we return the maximum element from the DP array which gives us the maximum score a team can achieve following the conflict rule.# Python Solution:

3class Solution:
4    def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
5        players = sorted(zip(ages, scores))
6        dp = [0] * len(players)
7        for i in range(len(players)):
8            dp[i] = players[i][1]
9            for j in range(i):
10                if players[j][1] <= players[i][1]:
11                    dp[i] = max(dp[i], dp[j] + players[i][1])
12        return max(dp)

The Python solution is quite similar to the C++ solution. This program first pairs the ages and scores of players together and sorts them in ascending order. A dynamic programming table, dp is used to find the best scores possible when including each player. zip() function is used to aggregate similar index of multiple containers so that they can be used just using as single entity.

JavaScript Solution:

3var bestTeamScore = function(scores, ages) {
4    let players = [];
5    for (let i = 0; i < scores.length; i++)
6        players[i] = [ages[i], scores[i]];
7    players.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
9    let dp = new Array(scores.length).fill(0);
10    for (let i = 0; i < players.length; i++) {
11        dp[i] = players[i][1];
12        for (let j = 0; j < i; j++)
13            if (players[j][1] <= players[i][1])
14                dp[i] = Math.max(dp[i], dp[j] + players[i][1]);
15    }
16    return Math.max(...dp);

The JavaScript solution first forms an array of pairs players that contains age-score pairs. We then sort players first by age then by score. After that, a dynamic programming array dp is used to find the maximum score that can be achieved when each player is taken.

Java Solution:

3import java.util.*;
5class Player {
6    int age;
7    int score;
9    public Player(int age, int score) {
10        this.age = age;
11        this.score = score;
12    }
15public class Solution {
16    public int bestTeamScore(int[] scores, int[] ages) {
17        List<Player> players = new ArrayList<>();
18        for (int i = 0; i < scores.length; i++)
19            players.add(new Player(ages[i], scores[i]));
21        Collections.sort(players, (a, b) -> (a.age != b.age) ? a.age - b.age : a.score - b.score);
23        int[] dp = new int[scores.length];
24        for (int i = 0; i < players.size(); i++) {
25            dp[i] = players.get(i).score;
26            for (int j = 0; j < i; j++)
27                if (players.get(j).score <= players.get(i).score)
28                    dp[i] = Math.max(dp[i], dp[j] + players.get(i).score);
29        }
30        Arrays.sort(dp);
31        return dp[dp.length - 1];
32    }

The Java solution uses a class Player to store the age and score of each player. This class also includes a constructor for creating new Player instances. The solution works by sorting the players according to age and then score, and putting the results in an array dp. The resulting array contains the maximum score obtainable by including each player in the team. The maximum element of this array is the answer to the problem.

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫