54. Best Team With No Conflicts
Maximum Sum longest increasing subsequence
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Explanation: You can choose all the players.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.Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation: It is best to choose the first 3 players. Solution: (Dp)
class Solution
{
public:
static bool comparator(pair<int, int> a, pair<int, int> b)
{
if (a.second == b.second)
{
return a.first < b.first;
}
return a.second < b.second;
}
int bestTeamScore(vector<int> &scores, vector<int> &ages)
{
int n = ages.size();
vector<pair<int, int>> v;
for (int i = 0; i < n; i++)
{
v.push_back({scores[i], ages[i]});
}
sort(v.begin(), v.end(), comparator);
vector<int> dp(n, 0);
int res = 0;
for (int i = 0; i < n; i++)
{
int score = v[i].first;
dp[i] = score;
for (int j = 0; j < i; j++)
{
if (v[j].second == v[i].second)
{
dp[i] = max(dp[i], score + dp[j]);
}
else if (v[j].second < v[i].second && v[j].first <= score)
{
dp[i] = max(dp[i], score + dp[j]);
}
}
res = max(res, dp[i]);
}
return res;
}
};Last updated