2020-09-01
486. 预测赢家
博弈型DP:
dp[i][j] 代表当前玩家在[i,j]以内与另一个玩家的分数之差的最大值.
class Solution: def PredictTheWinner(self, nums: List[int]) -> bool: n = len(nums) dp = [[0 for _ in range(n)] for _ in range(n)] ## dp[i][j] 代表当前玩家在[i,j]以内与另一个玩家的分数之差的最大值 def dfs(l, r): if l==r: dp[l][r] = nums[l] return nums[l] if dp[l][r]: return dp[l][r] ansl = nums[l] - dfs(l+1, r) ansr = nums[r] - dfs(l, r-1) dp[l][r] = max(ansl, ansr) return dp[l][r] dfs(0, len(nums)-1) return dp[0][len(nums)-1]>=0