Return different answers in "Run Code" and "Submit Solution"


  • 0
    W

    My code returns 1 for nums=[1] when I run the code with Custom Testcase, yet it returns 0 in OJ for the same input.

    class Solution(object):
    def rob(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums: return 0
        return self._rob(nums)
        
    def _rob(self,nums,i=0,d={}):
        if i in d:
            return d[i]
        if i>=len(nums)-2:
            d[i] = max(nums[i:])
            return d[i]
        d[i] = max(nums[i]+self._rob(nums,i+2,d),self._rob(nums,i+1,d))
        return d[i]

  • 0

  • 0
    W

    thanks! solved by defining the dictionary outside the function and feed in as explicit argument


Log in to reply
 

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