Simple Python Greedy solution


  • 0
    L
    class Solution(object):
        def canJump(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            last = len(nums)-1      #set last point
            i = last - 1        #set previous point
            while i >= 0:       #check whether the last point can backtrack to zero or not
                if nums[i] < last - i:           
                    i -= 1
                else:           #if satisfy the previous point's maxjump larger than distance between last point and prev point, set prev point as last point
                    last = i
                    i -= 1
            return last == 0
    

Log in to reply
 

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