Anyone knows why my code times out?


  • 0
    Z

    class Solution(object):
    def jump(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if len(nums) <= 1:
    return 0
    return self.recurse(nums,0)

    def recurse(self,nums,i):
        
        if nums[i]+i >= len(nums)-1:
            return 1
        min_jumps = 99999999
        for j in range(1,nums[i]+1):
            tmp = self.recurse(nums,i+j)
            min_jumps = min(min_jumps,tmp)
        #print(min_jumps+1)
        return min_jumps+1

  • 0
    Z

    Sorry for the formatting issue.

    class Solution(object):
        def jump(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) <= 1:
                return 0
            return self.recurse(nums,0)
        
        def recurse(self,nums,i):
            
            if nums[i]+i >= len(nums)-1:
                return 1
            min_jumps = 99999999
            for j in range(1,nums[i]+1):
                tmp = self.recurse(nums,i+j)
                min_jumps = min(min_jumps,tmp)
            #print(min_jumps+1)
            return min_jumps+1
    

Log in to reply
 

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