Best Team With No Conflicts

Best Team With No Conflicts

update Oct 18th, 2020

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 1:

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

Example 2:

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.

Example 3:

Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation: It is best to choose the first 3 players.

Constraints:

1. 1 <= scores.length, ages.length <= 1000
2. scores.length == ages.length
3. 1 <= scores[i] <= 106 
4. 1 <= ages[i] <= 1000

Basic Idea

比较容易想到的第一步是按照年龄排序,进而可以想到如果年龄相同需要按照分数排序。考虑到需要保证选择的人之中不能出现分数更高但是更年轻的选手,而选择或者不选择某个选手会对之后的选择产生影响,整个思路倾向于DP,考虑和LIS(Longest Increasing Subsequence)类似的O(N^2)解法。我们定义如下DP规则:

Java Code:

从左往右:

Last updated