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

最近在整理课本第六章动态规划的课后习题,就顺便找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& 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第一次选的牌的数。

你可能感兴趣的:(LeetCode)