Predict the Winner

来源:互联网 发布:河北农业科技网络书屋 编辑:程序博客网 时间:2024/06/05 20:44

Description
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 <= length of the array <= 20.
Any scores in the given array are non-negative integers and will not exceed 10,000,000.
If the scores of both players are equal, then player 1 is still the winner.
解题思路:因为player只能选择最左边或者最右边的数,所以player1选数时,无非只有两种选择,我们可以这样来考虑问题,假设数组为a[i, j],player1要么选择a[i],要么选择a[j],选择的依据是使在player2选了数之后,在剩下的数组里面,player1能得到最大的score,现在问题就变成了原问题的一个子问题。但是怎样找到原问题与子问题的关系?这不仅仅是有关player1的问题,还有关player2的问题。当player1选择a[i]时,player2的选择为a[i + 1],a[j],当player2选择a[j]时,player2的选择为a[i],a[j - 1],因为player2的目标就是使player1的score最小,所以player2要做的就是选择之后能让player1的score达到最小。设player1的score可以由f(a[i , j])求出,那么f(a[i, j])=max(a[i] + min(f(a[i + 2, j]), f(a[i + 1, j - 1])), a[j] + min(f(a[i , j - 1]), f(a[i + 1, j - 1])))。程序代码如下:

class Solution {public:    int scores(vector<int> & nums) {        int size = nums.size();        if (size == 0)            return 0;        if (size == 1)            return nums[0];        if (size == 2)            return max(nums[0], nums[1]);        vector<int> temp1(nums);         vector<int> temp2(nums);        temp1.erase(temp1.begin());                               //a[i+1,j]        temp2.erase(temp2.begin() + temp2.size() - 1);            //a[i,j-1]        vector<int> temp3(temp1);        vector<int> temp4(temp1);        vector<int> temp5(temp2);        vector<int> temp6(temp2);        temp3.erase(temp3.begin());                               //a[i+2,j]        temp4.erase(temp4.begin() + temp4.size() - 1);            //a[i+1,j-1]        temp5.erase(temp5.begin());                               //a[i+2,j-1]        temp6.erase(temp6.begin() + temp6.size() - 1);            //a[i+1,j-1]        return max(nums[0] + min(scores(temp3), scores(temp4)),             nums[size - 1] + min(scores(temp5), scores(temp6)));}    bool PredictTheWinner(vector<int>& nums) {        int size = nums.size();        int sum = 0;        for (int i = 0; i < size; i++) {            sum += nums[i];        }        return scores(nums) * 2 >= sum;      //不小于总和的一半,player1胜出    }};
0 0
原创粉丝点击