LeetCode 486. Predict the Winner

来源:互联网 发布:免费公司记账软件 编辑:程序博客网 时间:2024/06/05 02:54

【题目】

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: FalseExplanation: 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: TrueExplanation: 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.

【题解】

 这是一道动态规划的题目,解题思路是参考网上大神而来。

dp[i][j] 表示能够在i到j之间获得的所有数字之和的最大值,目标是第一个人能否在0到n-1之间的所有数字中取得数字之和大于数组所有元素之和的1/2

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

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

dp[i][j] = nums[i] + sum[i+1][j] - dp[i+1][j] 
同理,当第一个人取第j个元素之后,有

dp[i][j] = nums[j] + sum[i][j-1] - dp[i][j-1]

动态转移方程如下:

当第一个人获取第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)

【代码】

    bool PredictTheWinner(vector<int>& nums) {        vector<std::vector<int> > dp(nums.size(), std::vector<int>(nums.size()));        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];        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();    }



原创粉丝点击