Best Team With No Conflicts

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:

Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Explanation: You can choose all the players.

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

int bestTeamScore(vector<int&scoresvector<int&ages)
{
    vector<pair<intint>> vec;
    for (int i = 0; i < ages.size(); i++)
        vec.push_back(make_pair(ages[i], scores[i]));

    //sort the array
    sort(vec.begin(), vec.end());
    vector<intdp(ages.size());
    int ans = 0, n = vec.size();

    //iterate for all the elements of the array
    for (int i = 0; i < n; i++)
    {

        dp[i] = vec[i].second;
        for (int j = 0; j < i; j++)
        {
            if (vec[j].second <= vec[i].second)
            {
                dp[i] = max(dp[i], dp[j] + vec[i].second);
            }
        }
        ans = max(ans, dp[i]);
    }
    return ans;
}

int main()
{
    vector<int> scores = {1351015};
    vector<int> ages = {12345};

    cout << bestTeamScore(scores, ages);

    return 0;
}


No comments:

Post a Comment