Description
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Constraints:
- 2 <= piles.length <= 500
- piles.length is even.
- 1 <= piles[i] <= 500
- sum(piles) is odd.
分析
题目的意思是:给定一个数组,Alex首先开始选择数,只能在开头或者末尾选择,两个人的选择都是最优的,问Alex能否赢。这道题我参考了一下别人的思路.
- 设置dp数组,定义二维数组dp,其行数和列数都等于石子的堆数,dp[i][j] 表示当剩下的石子堆为下标 i到下标 j 时,当前玩家与另一个玩家的石子数量之差的最大值,注意当前玩家不一定是先手 Alex。
- 只有当 i≤j 时,剩下的石子堆才有意义,因此当i>j 时,dp[i][j]=0。
- 当 i=j 时,只剩下一堆石子,当前玩家只能取走这堆石子
- 当 i<j 时,当前玩家可以选择取走 piles[i] 或 piles[j],然后轮到另一个玩家在剩下的石子堆中取走石子。在两种方案中,当前玩家会选择最优的方案,使得自己的石子数量最大化。递推公式为:
为了更直观的看dp里面是啥,我把例子的dp数组打印了出来:
代码
参考文献
[LeetCode] DP or Just return true
[LeetCode] 877. Stone Game 石子游戏