486. Predict the Winner

15 篇文章 0 订阅

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.

【问题分析】

 1、该问题是两个人交替从一个数组两端获取一个数字,每次每个人只能获取一个,最终获取的数字之和最大的获胜,问第一个人能否获胜

 2、该问题的解法有两种: 

        一种是建立二叉树方法:

        A、建立两颗二叉树,第一颗的根节点是第一个数字,第二颗的根节点是第二个数字

        B、建树过程每次从当前剩余元素中取最左端和最右端的元素,分别作为当前节点的左孩子和右孩子,依次递归建立二叉树

        C、两棵树从根节点到叶节点的路径总个数为2^(n-1)个,遍历所有路径,依次计算路径中1、3、5...个节点之和,

        D、如果大于所有节点只和的一半,或者大于2、4、6...节点数字只和,则第一个取数字者可以获胜,否则无法获胜

        另一种是动态规划算法:

        A、问题定义是:从数组下标0到n-1之间取数字的规则是交替进行,第一个人从数组首或尾取一个,第二个人从剩余数组元素首或尾取一个,

               依次交替直到取完所有数字

        B、定义dp[i][j] 表示数组下标i到j之间取数字为子问题,它等于能够在i到j之间获得的所有数字之和和的最大值

               目标是第一个人能否在0到n-1之间的所有数字中取得数字只和大于数组所有元素之和的1/2,即dp[0][n-1]

        C、dp[i][j]表示第一个人能够从i到j之间获取的所有数字之和的最大值,那么

               dp[i+1][j]表示第二个人能够在i-1到j之间获取的所有数字之和的最大值,

               dp[i][j-1]表示第二个人能够在i到j-1之间获取的所有数字之和的最大值

        D、sum[i+1][j] - dp[i+1][j]表示第一个人取第i个元素以后,能够在i+1到j之间获取子数组之和的最大值

               sum[i][j-1] - dp[i][j-1]标识第一个人取第j个元素以后,能够在i到j-1之间获取子数组之和的最大值

               由于dp[i][j]表示每个人都试图获取当前下标i到j之间字数组之和最大值,

               因此当剩余数组下标为i到j时,

               当第一个人取第i个元素之后,第二个人也在试图获取i+1到j之间的子数组之和最大dp[i+1][j],此时第一个人能够在i到j之间获取的最终子数组之和为

               nums[i] + sum[i+1][j] - dp[i+1][j]           // 后半部分表示第二个人获取最大子数组以后,剩余的字数组之和,也是第一个人只能选择的子数组


               递推公式如下:

               当第一个人获取第i个元素时,dp[i][j] = nums[i] + sum[i+1][j] - dp[i+1][j] = dp_left

               当第一个人获取第j个元素时,dp[i][j] = nums[j] + sum[i][j-1] - dp[i][j-1]  = dp_right

               第一个人想获胜,因此dp[i][j] = max(dp_left, dp_right)

               当i == j时,只有一种选择,dp[i][j] = nums[i]

               当i == j-1 时,只有两种选择,dp[i][j] = max(nums[i], nums[j])


               i到j之间的数组之和可以表示为sum[i][j] = sum[0][j] - sum[0][i-1] = sum(j) - sum(i-1)

               

【AC代码】

class Solution3 {
    public:
        bool PredictTheWinner(std::vector<int>& nums) {
            std::vector<std::vector<int> > dp(nums.size(), std::vector<int>(nums.size()));
            std::vector<int> prefix_sum(nums.size()+1);
            prefix_sum[0] = 0;

            for (int i = 0; i < nums.size(); ++i) {
                prefix_sum[i+1] = prefix_sum[i] + nums[i]; //下标从i+1开始
            }

            for (int len = 1; len <= nums.size(); ++len) {
                for(int lhs = 0; lhs + len - 1 < nums.size(); ++lhs) {
                    int rhs = lhs + len - 1;
                    if (lhs == rhs) {
                        dp[lhs][rhs] = nums[lhs];
                    }
                    else if(lhs == rhs - 1) {
                        dp[lhs][rhs] = std::max(nums[lhs], nums[rhs]);
                    }
                    else {
                        int dp_left = nums[lhs] + prefix_sum[rhs+1] - prefix_sum[lhs+1] - dp[lhs+1][rhs];
                        int dp_right = nums[rhs] + prefix_sum[rhs] - prefix_sum[lhs] - dp[lhs][rhs-1];
                        dp[lhs][rhs] = std::max(dp_left, dp_right);
                    }
                }
            }
            return 2 * dp[0][nums.size()-1] >= prefix_sum.back();
            //return dp[0][nums.size()-1] >= prefix_sum.back() / 2 + prefix_sum.back() % 2;
        }
};

参考资料:

https://discuss.leetcode.com/topic/76327/c-dp-solution-with-explanation

https://discuss.leetcode.com/topic/76472/clean-3ms-c-dp-solution-with-detailed-explanation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值