LeetCode----486. Predict the Winner 动态规划

这篇博客探讨了LeetCode中的486题,玩家1和玩家2根据动态规划策略选择数组中的非负整数得分,目标是预测哪位玩家能赢得比赛。通过分析,博主得出结论:当序列数为偶数时,玩家1总能获胜;奇数时,玩家1的胜负取决于首张选取的分数。并提供了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在整理课本第六章动态规划的课后习题,就顺便找leetcode上的题目做啦,顺便复习。。

1 题目

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.

Example 1:

Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2. 
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.

Note:

  1. 1 <= length of the array <= 20.
  2. Any scores in the given array are non-negative integers and will not exceed 10,000,000.
  3. If the scores of both players are equal, then player 1 is still the winner.
值得注意的是第三点,平局也算赢

 链接:点击打开链接

2 分析

 dp[i][j]表示玩家1从i到j的能选到的最大值之和,初始为0

状态转移方程为:dp[i][j] = max(nums[i] + min(dp[i + 1][j - 1], dp[i + 2][j]), nums[j] + min(dp[i + 1][j - 1], dp[i][j - 2]));

解释:因为玩家1要么选择nums[i],要么选择nums[j],所以有两种情况。

note:2个玩家用的都是最优策略

如果玩家1选择了nums[i],那么他接下来的选择的数字则是[i + 1.....j]之间的数字中除去玩家2选择的数字。

而玩家2选择的情况也有两种,由于玩家2用的也是最优策略,所以玩家1只能选[i + 1.....j]之间的数字中较小的情况。

如果玩家1选择了nums[j],同理。

结果即dp[0][n - 1],若结果大于等于序列数之和的一半,则说明玩家1赢了

3 C++实现

class Solution {
public:
    //note:平局也返回true
    bool PredictTheWinner(vector<int>& nums) {
        
        //dp[i][j]表示玩家1从i到j的能选到的最大值之和
        int n = nums.size();
        
        int dp[n][n] = {0};
        
        //当只有一个数或两个数时,玩家1因为先选,肯定能赢(或平局)
        if(n == 1 || n == 2)
            return true;
        
        //计算整个序列的数之和
        int sum = 0;
        for(auto a : nums)
            sum += a;
            
        for(int i = 0; i < n - 1; i++) {
            //len = 1的情况
            dp[i][i] = nums[i];
            //len = 2的情况
            dp[i][i + 1] = max(nums[i], nums[i + 1]);
        }
        dp[n - 1][n - 1] = nums[n - 1];
        
        //从len = 3开始枚举
        for(int len = 3; len <= n; len++) {  
            for(int i = 0; i + len <= n; i++) {
                int j = i + len - 1;
                dp[i][j] = max(nums[i] + min(dp[i + 1][j - 1], dp[i + 2][j]), nums[j] + min(dp[i + 1][j - 1], dp[i][j - 2]));
            }
        }
    
        //若玩家1选的值之和大于等于全部和的一半,则胜出(或平局),sum + 1是为了处理sum为基数的情况,如:[1,3,1]
        if(dp[0][n - 1] >= (sum + 1) / 2)
            return true;
        else return false;
        
    }
};

觉得代码有些冗余,实现起来有点笨,但是思想就是这样啦

有兴趣可以看leetcode上的讨论,链接:点击打开链接

4 思考

和同学讨论的时候有同学提到序列个数的奇偶对结果的影响:

① 如果序列数个数是偶数,则玩家1一定可以赢。因为玩家1可以设法选到自己想要的牌,不过怎么证明呢?

例如:[1,2,3,1],玩家1要选到3,则他一开始选位于首位的1,此时序列变成了[2,3,1],玩家2只能选择2,他选择不到3这个数字。最后玩家1赢。

② 如果序列数个数是奇数,则不一定,玩家1的输赢取决于他的第一张牌如何。因为玩家1不一定能选到自己想要的牌。如:1,100, 2,两个玩家都想选到100,但是玩家1做不到。
那如果①成立的话,在情况②里,除掉玩家1选择了第一张牌以后,剩下的就是偶数张牌了,我们可以把这个剩下的情况看成情况①,意思就是玩家2一定会赢罗,那这局比赛的结果就取决于玩家1第一次选的牌的数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值