Skip to content

Latest commit

 

History

History
190 lines (159 loc) · 5.09 KB

File metadata and controls

190 lines (159 loc) · 5.09 KB

中文文档

Description

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 <= scores.length, ages.length <= 1000
  • scores.length == ages.length
  • 1 <= scores[i] <= 106
  • 1 <= ages[i] <= 1000

Solutions

LIS.

Python3

class Solution:
    def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
        nums = list(zip(ages, scores))
        nums.sort()
        n = len(nums)
        dp = [num[1] for num in nums]
        for i in range(n):
            for j in range(i):
                if nums[i][1] >= nums[j][1]:
                    dp[i] = max(dp[i], dp[j] + nums[i][1])
        return max(dp)

Java

class Solution {
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = ages.length;
        int[][] nums = new int[n][2];
        for (int i = 0; i < n; ++i) {
            nums[i] = new int[] {ages[i], scores[i]};
        }
        Arrays.sort(nums, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]; });
        int[] dp = new int[n];
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            dp[i] = nums[i][1];
            for (int j = 0; j < i; ++j) {
                if (nums[i][1] >= nums[j][1]) {
                    dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
                }
            }
            ans = Math.max(ans, dp[i]);
        }
        return ans;
    }
}

C++

class Solution {
public:
    int bestTeamScore(vector<int>& scores, vector<int>& ages) {
        int n = ages.size();
        vector<vector<int>> nums(n);
        for (int i = 0; i < n; ++i) nums[i] = {ages[i], scores[i]};
        sort(nums.begin(), nums.end());
        vector<int> dp(n);
        for (int i = 0; i < n; ++i) {
            dp[i] = nums[i][1];
            for (int j = 0; j < i; ++j) {
                if (nums[i][1] >= nums[j][1])
                    dp[i] = max(dp[i], dp[j] + nums[i][1]);
            }
        }
        return *max_element(dp.begin(), dp.end());
    }
};

Go

func bestTeamScore(scores []int, ages []int) int {
	n := len(ages)
	nums := make([][]int, n)
	for i, age := range ages {
		nums[i] = []int{age, scores[i]}
	}
	sort.Slice(nums, func(i, j int) bool {
		if nums[i][0] != nums[j][0] {
			return nums[i][0] < nums[j][0]
		}
		return nums[i][1] < nums[j][1]
	})
	dp := make([]int, n)
	ans := 0
	for i, num := range nums {
		dp[i] = num[1]
		for j := 0; j < i; j++ {
			if num[1] >= nums[j][1] {
				dp[i] = max(dp[i], dp[j]+num[1])
			}
		}
		ans = max(ans, dp[i])
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

JavaScript

/**
 * @param {number[]} scores
 * @param {number[]} ages
 * @return {number}
 */
var bestTeamScore = function (scores, ages) {
    const nums = ages.map((age, i) => [age, scores[i]]);
    nums.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
    const n = nums.length;
    let dp = new Array(n);
    let ans = 0;
    for (let i = 0; i < n; ++i) {
        dp[i] = nums[i][1];
        for (let j = 0; j < i; ++j) {
            if (nums[i][1] >= nums[j][1]) {
                dp[i] = Math.max(dp[i], dp[j] + nums[i][1]);
            }
        }
        ans = Math.max(ans, dp[i]);
    }
    return ans;
};

...