Python simple recursive, easy to read

  • 0
    class Solution(object):
        def PredictTheWinner(self, nums):
            :type nums: List[int]
            :rtype: bool
            self.nums = nums
            self.memo = {}
            return self.isWinner(0,len(nums)-1, 1)>=0
        def isWinner(self,i,j,sign):
            if (i,j) in self.memo:
                return self.memo[(i,j)]
            elif i==j: 
                return sign*self.nums[i]
                l = sign*self.nums[i] + self.isWinner(i+1,j,-1*sign)
                r = sign*self.nums[j] + self.isWinner(i,j-1,-1*sign)
                val = sign*max(sign*l,sign*r)
                return val

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.