【每日一题Day154】LC1626无矛盾的最佳球队 | 动态规划

简介: 【每日一题Day154】LC1626无矛盾的最佳球队 | 动态规划

无矛盾的最佳球队【LC1626】

假设你是球队的经理。对于即将到来的锦标赛,你想组合一支总体得分最高的球队。球队的得分是球队中所有球员的分数 总和

然而,球队中的矛盾会限制球员的发挥,所以必须选出一支 没有矛盾 的球队。如果一名年龄较小球员的分数 严格大于 一名年龄较大的球员,则存在矛盾。同龄球员之间不会发生矛盾。

给你两个列表 scoresages,其中每组 scores[i]ages[i] 表示第 i 名球员的分数和年龄。请你返回 所有可能的无矛盾球队中得分最高那支的分数

原本的思路想复杂了,虽然写的也是动态规划也排序了,用dfs回溯写了一版,每个球员有选或者不选两种可能,记录了小于等于当前年龄的最大分数,如果当前分数大于等于该值,才可以选择,然后超时。于是加记忆化,但是记忆化卡在了一个案例,没空就错了,代码放在下面,有兴趣的友友可以看看,也许思路就不大对吧

  • 思路

image.png

4.确定遍历顺序
从前往后遍历

5.举例推导dp数组

class Solution {
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length;
        int[][] players = new int[n][2];
        for (int i = 0; i < n; i++){
            players[i][0] = ages[i];
            players[i][1] = scores[i];
        }
        Arrays.sort(players, new Comparator<int[]>(){
            public int compare(int[] o1, int[] o2){
                if (o1[0] == o2[0]){
                    return o1[1] - o2[1];
                }
                return o1[0] -o2[0];
            }
        });
        int[] dp = new int[n + 1];
        int res = 0;
        for (int i = 1; i <= n; i++){
            for (int j = 0; j < i; j++){
                if (j == 0 || players[i - 1][1] >= players[j - 1][1]){
                    dp[i] = Math.max(players[i - 1][1] + dp[j], dp[i]);
                }
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}

image.png

代码学习:

将下标根据值排序

class Solution {
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length, ans = 0;
        var ids = new Integer[n];
        for (int i = 0; i < n; ++i)
            ids[i] = i;
        Arrays.sort(ids, (i, j) -> scores[i] != scores[j] ? scores[i] - scores[j] : ages[i] - ages[j]);
        var f = new int[n + 1];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j)
                if (ages[ids[j]] <= ages[ids[i]])
                    f[i] = Math.max(f[i], f[j]);
            f[i] += scores[ids[i]];
            ans = Math.max(ans, f[i]);
        }
        return ans;
    }
}
作者:灵茶山艾府
链接:https://leetcode.cn/problems/best-team-with-no-conflicts/solutions/2183029/zui-chang-di-zeng-zi-xu-lie-cong-on2-dao-ojqu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

回溯 错误代码

有点01背包的感觉,背包的容量为年龄和分数

class Solution {
    int[][] dp;
    public int bestTeamScore(int[] scores, int[] ages) {
        int n = scores.length;
        int[][] players = new int[n][2];
        int maxScore = 0;
        int maxAge = 0;
        for (int i = 0; i < n; i++){
            maxAge = Math.max(maxAge, ages[i]);
            maxScore = Math.max(maxScore, scores[i]);
            players[i][0] = ages[i];
            players[i][1] = scores[i];
        }
        dp = new int[maxScore + 1][maxAge + 1];
        for (int i = 0; i <= maxScore; i++){
            Arrays.fill(dp[i], -1);
        }
        Arrays.sort(players, new Comparator<int[]>(){
            public int compare(int[] o1, int[] o2){
                if (o1[0] == o2[0]){
                    return o1[1] - o2[1];
                }
                return o1[0] -o2[0];
            }
        });
        return dfs(players, 0, 0, 0);
    }
    public int dfs(int[][] players, int i, int maxScore, int age){
        if (i == players.length) return 0;
        if (maxScore > 0 && age > 0 && dp[maxScore][age] != -1) return dp[maxScore][age];
        // 不选
        int score = dfs(players, i + 1, maxScore, age);
        // 选 
        if (age == players[i][0] || (age < players[i][0] && players[i][1] >= maxScore)){
            int curScore = maxScore;
            int curAge = age;
            if (curScore <= players[i][1]){
                curScore = players[i][1];
                curAge = players[i][0]; 
            }
            score = Math.max(score, players[i][1] + dfs(players, i + 1, curScore, curAge));
        }
        dp[maxScore][age] = score;
        return score;
    }
}


目录
相关文章
|
5月前
|
机器学习/深度学习
【每日一题Day125】LC1326灌溉花园的最少水龙头数目 | 动态规划 贪心
【每日一题Day125】LC1326灌溉花园的最少水龙头数目 | 动态规划 贪心
31 0
|
5月前
【每日一题Day334】LC2591将钱分给最多的儿童 | 贪心
【每日一题Day334】LC2591将钱分给最多的儿童 | 贪心
22 0
|
5月前
【每日一题Day282】LC2681英雄力量 | 排序+数学
【每日一题Day282】LC2681英雄力量 | 排序+数学
17 0
|
4月前
蓝桥备战--纪念品分组OJ532,贪心证明
蓝桥备战--纪念品分组OJ532,贪心证明
14 0
|
4月前
蓝桥备战--分糖果OJ2928 贪心 分类讨论
蓝桥备战--分糖果OJ2928 贪心 分类讨论
36 0
|
5月前
|
机器人
【每日一题Day343】LC2731移动机器人 | 脑筋急转弯+数学
【每日一题Day343】LC2731移动机器人 | 脑筋急转弯+数学
25 0
|
5月前
【每日一题Day338】LC2582递枕头 | 模拟+数学
【每日一题Day338】LC2582递枕头 | 模拟+数学
11 0
|
5月前
【每日一题Day126】LC1140石子游戏Ⅱ | 博弈dp 记忆化搜索
【每日一题Day126】LC1140石子游戏Ⅱ | 博弈dp 记忆化搜索
30 0
|
7月前
|
算法 C++
剑指offer(C++)-JZ47:礼物的最大价值(算法-动态规划)
剑指offer(C++)-JZ47:礼物的最大价值(算法-动态规划)
|
9月前
三道华为机试题
三道华为机试题
34 0