【leetcode】-877. Stone Game石子游戏 动态规划


#题目
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:

Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.

智力解法

参考了别人的答案,发现不管怎么样都是先手获胜,直接返回True就能通过。

class Solution(object):
    def stoneGame(self, piles):
        """
        :type piles: List[int]
        :rtype: bool
        """
        return True

动态规划

老老实实用动态规划的方法来做吧。假设两个人都很聪明,返回最后得分之差。如果先手分数大于后手,先手胜;否则,后手胜。

首先定义dp[i][j]:对piles中下标从i到j区间的数进行操作。

需要另外用长度为2的数组表示游戏中的先手和后手。那么定义一个三维数组,dp[i][j][0]表示,对于 piles[i…j] 这部分石头堆,先手能获得的最高分数;dp[i][j][1] 表示,对于 piles[i…j] 这部分石头堆,后手能获得的最高分数。

根据前面对 dp 数组的定义,状态显然有三个:开始的索引 i,结束的索引 j,当前轮到的人.
初始化:
dp[i][j][0] = piles[i]
dp[i][j][1] = 0
其中 0 <= i == j < n
解释:i 和 j 相等就是说面前只有一堆石头 piles[i],那么显然先手的得分为 piles[i];后手没有石头拿了,得分为 0。

状态转移方程:
dp[i][j][0] = max(piles[i] + dp[i+1][j][1], piles[j] + dp[i][j-1][1])
解释:我作为先手,面对 piles[i…j] 时,有两种选择:
1.我选择最左边的那一堆石头,然后面对 piles[i+1…j],但是此时轮到对方,相当于我变成了后手;
2.我选择最右边的那一堆石头,然后面对 piles[i…j-1], 但是此时轮到对方,相当于我变成了后手。

如果先手选择左边:
dp[i][j][1] = dp[i+1][j][0]
if 先手选择右边:
dp[i][j][1] = dp[i][j-1][0]
解释:我作为后手,要等先手先选择,有两种情况:
1.如果先手选择了最左边那堆,给我剩下了 piles[i+1…j],此时轮到我,我变成了先手;
2.如果先手选择了最右边那堆,给我剩下了 piles[i…j-1],此时轮到我,我变成了先手。

最后先手获得分数为dp[0][n-1][0],后手获得分数为dp[0][n-1][1].如果先手分数大于后手,先手胜;否则,后手胜。

python代码

class Solution(object):
    def stoneGame(self, piles):
        """
        :type piles: List[int]
        :rtype: bool
        """
        n = len(piles)
        dp = [[[0]*2 for j in range(n)] for i in range(n)]
        for i in range(n):
            dp[i][i][0] = piles[i]
            dp[i][i][1] = 0
        for l in range(2,n+1):
            for i in range(n-l+1):
                j = l + i - 1
                left = piles[i] +dp[i+1][j][1]
                right = piles[j]+dp[i][j-1][1]
                if left>right:
                    dp[i][j][0] = left
                    dp[i][j][1] = dp[i+1][j][0]
                else:
                    dp[i][j][0] = right
                    dp[i][j][1] = dp[i][j-1][0]
        res = dp[0][n-1]
        if res[0]-res[1]>0:
            return True
        else:
            return False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值