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:
Example 2:
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.
解法1:
是同时维护两个dp而相互update的一类题目。
用max,min分别表示对于[i,j]范围的一组数,先手的player能取到的最大和最小值。
要注意: min对于[i,i]就为0
max对于[i,j]的递推公式是,如果取尾巴,那么再取下一步一定是取[i, j -1]的最小值。
如果取头部,那么再取一定是取[i - 1, j]的最小值。
min对于[i, j]的递推公式是,当对手取了[i]或者[j]之后,剩下的矩阵取一个最大值.
要注意更新矩阵的时候是按对角线更新。
C++
Java