题目:
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.
思路:
隐隐约约记得这道题目好像我原来实验室的同事在Google onsite的时候遇到过,当时我们还讨论过。
我们定义dp[i][j]表示在[i, j]区间内,Player1可以获得的最大分数。我们的最终目标是找出Player1在区间[0, n - 1]内是否可以获得总分数的一半及以上。需要注意的是,由于Player1和Player2是轮流取数的,所以这里面的递推关系是:如果dp[i][j]表示Player1可以在区间[i, j]内获得的最大分数,那么dp[i +1][j]表示Player2在区间[i + 1, j]内可以获得的最大分数,dp[i][j - 1]同理。
由以上表述可知:sum[i + 1][j] - dp[i + 1][j]就表示Player1在选择nums[i]之后,最终在区间[i + 1, j]内可以获得的最大分数。同理可以推导出Player1选择nums[j]之后的情况。因此最终我们可以推导出如下的递推关系:
pickRight = nums[j] + sum[i][j - 1] - dp[i][j - 1] // 如果Player1选择了右侧的nums[j];
dp[i][j] = max(pickLeft, pickRight).
而平凡情况是i == j以及i + 1 == j,可以分别作为两种特殊情况进行处理:
dp[i][j] = nums[i] // 如果i == j
dp[i][j] = max(nums[i], nums[j) // 如果i + 1 == j
为了降低时间复杂度,可以记录nums的前缀和,这样任意区间和就可以在O(1)的时间复杂度之内得到:sum[i][j] = prefixSum[j] - prefixSum[i-1]。
代码:
class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> score(n, vector<int>(n, 0));
vector<int> prefixSum(n + 1);
prefixSum[0] = 0;
for (int i = 0; i < n; i++) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
for (int len = 1; len <= n; ++len) {
for (int l = 0; l + len - 1 < n; ++l) {
int r = l + len - 1;
if (l == r) { // special case 1: only one element
score[l][r] = nums[l];
}
else if (l == r - 1) { // special case 2: two elements
score[l][r] = max(nums[l], nums[r]);
}
else {
int pickLeft = nums[l] + prefixSum[r+1] - prefixSum[l+1] - score[l+1][r];
int pickRight = nums[r] + prefixSum[r] - prefixSum[l] - score[l][r-1];
score[l][r] = max(pickLeft, pickRight);
}
}
}
return score[0][n - 1] >= prefixSum.back() / 2 + prefixSum.back() % 2;
}
};