leetcode-5379-石子游戏③

题目描述:

leetcode-5379-石子游戏③_第1张图片

 

 方法:从后向前dp

class Solution(object):
    def stoneGameIII(self, A):
        n = len(A)
        dp = [-float('inf')] * n
        for i in range(n-1,-1,-1):
            dp[i] = max(dp[i], sum(A[i:i+1]) - (dp[i+1] if i+1else 0))
            dp[i] = max(dp[i], sum(A[i:i+2]) - (dp[i+2] if i+2else 0))
            dp[i] = max(dp[i], sum(A[i:i+3]) - (dp[i+3] if i+3else 0))
        if dp[0] == 0:return "Tie"
        if dp[0] > 0:return "Alice"
        if dp[0] < 0:return "Bob"

 

你可能感兴趣的:(leetcode-5379-石子游戏③)