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 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.

思路:首先按照年龄排序,然后按照score排序,最后就是求longest increasing subsequence,用DP解决,递推公式就是j < i, 并且score[j] <= score[i] 就可以加上当前的score , dp[i] = Math.max(dp[i], dp[j] + nodes[i].score);

class Solution {
    private class Node {
        public int score;
        public int age;
        public Node (int score, int age) {
            this.score = score;
            this.age = age;
        }
    }
    
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length;
        Node[] nodes = new Node[n];
        for(int i = 0; i < n; i++) {
            nodes[i] = new Node(scores[i], ages[i]);
        }
        Arrays.sort(nodes, (a, b) -> (a.age != b.age ? a.age - b.age : a.score - b.score));
        int[] dp = new int[n];
        int res = 0;
        for(int i = 0; i < n; i++) {
            dp[i] = nodes[i].score;
            for(int j = 0; j < i; j++) {
                if(nodes[j].score <= nodes[i].score) {
                    dp[i] = Math.max(dp[i], dp[j] + nodes[i].score);
                }
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值