Skip to content
LC-0486 Medium LeetCode

486. Predict the Winner

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 56% Topics: Array, Math, Dynamic Programming, Recursion, Game Theory
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^2)
# Space: O(n)

class Solution(object):
    def PredictTheWinner(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if len(nums) % 2 == 0 or len(nums) == 1:
            return True

        dp = [0] * len(nums)
        for i in reversed(xrange(len(nums))):
            dp[i] = nums[i]
            for j in xrange(i+1, len(nums)):
                dp[j] = max(nums[i] - dp[j], nums[j] - dp[j - 1])

        return dp[-1] >= 0


Solution from kamyu104/LeetCode-Solutions · MIT