LeetCode 486. Predict the Winner

来源:互联网 发布:生产玩具知乎 编辑:程序博客网 时间:2024/06/04 19:00

486. Predict the Winner

一、问题描述

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.

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.

二、输入输出

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.

三、解题思路

动态规划-自顶向下带备忘

  • 这道题在MIT 6046的公开课上讲过。如果输入数组的个数是偶数的话,根本不用动态规划,先走的人一定可以保证不会输。先走的人只需要把全部偶数位置的数和算一遍,再把全部奇数位置的数的和算一遍,按照较大的那一个走就一定会赢。因为总数是偶数个,所以对手是没有办法阻止你一直选奇数位置的数或者一直选偶数位置的数的。
  • 现在的问题是总数可能是奇数的。就不能保证你一直可以选到奇数位置或者偶数位置的数了。MIT公开课上讲的DP方法就可以拿来用了。我们求出我们能获得的最大和,让总和减去我们能获得的最大的。如果这样都不能赢的话,那么肯定赢不了了。
  • 一堆数 v1, v2, v3 … vn 我们定义V(i,j)表示从数组 [vi, vi+1, …, vj] 中能取到的最大和. i <= j
  • 难点就在于我们需要知道对手怎么选的,才能知道下一步我们怎么选。好,在DP中遇到这种问题,默认对手会选择最优策略,就是会跟你一样聪明的去选择。那么他留给你的一定是比较差的那一个 或者说是 你一定被剩下最坏的情况所以状态转移方程为V(i,j) = max{ min{V(i+1,j-1), V(i+2,j)} + nums[i], min{V(i+1, j-1), V(i, j-2)} + nums[j] } 外面的max是说我会选取让我最大的那种方法(是选i 还是选j 能让我的收益最大) 里面的两个min是说,无论我第一步选择i还是j 那么对手留给我的一定是较差的那种情况。
  • Base case V(i, i) = nums[i]; V(i, j) = max{ nums[i], nums[j] } where i+1 == j;
class Solution {public:        vector<vector<int> > caches;    int DP_win(const vector<int>& nums, int i, int j)    {        if(caches[i][j] >=0 )return caches[i][j];        if(i>j) return 0;        if(i == j) return nums[i];        if(j == (i+1)) return max(nums[i], nums[j]);        int maxI = min(DP_win(nums, i+1, j-1), DP_win(nums, i+2, j)) + nums[i]; //iff I pick i        int maxJ = min(DP_win(nums, i+1, j-1), DP_win(nums, i, j-2)) + nums[j];//iff I pick j        caches[i][j] = max(maxI, maxJ);        return caches[i][j];    }    bool PredictTheWinner(vector<int>& nums) {        if(nums.size() == 0 || nums.size() == 1) return true;        int sum = accumulate(nums.begin(), nums.end(), 0);        for (int i = 0, n = nums.size(); i < n; ++i) {            vector<int> tmp(n, -1);            caches.push_back(tmp);        }        int myMax = DP_win(nums, 0, nums.size()-1);        if(myMax >= (sum-myMax)) return true;        return false;    }};