leetcode-mid-dynamic programming-55. Jump Game

mycode  71.47%

Ideas:

If it wants to reach the end, you can Anjiu backwards, in order to reach n, you can have the following situation

1) to the n-1, then this position can at least take a step

2) to the n-2, then this position can at least take two steps

3) reaches the n-3, and this position can at least take three steps

。。。。

 emmmm ... had wanted to write a function in accordance with this method, we found nowhere to run. . . . I'll just put upside down list, the number is greater than equal to the moment, he can reach a number, but when this number is temporarily unable to reach, I need to add a number to the next step of it requires a

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        nums[:] = nums[::-1]
        length = len(nums)
        step = 1
        for i in range(1,length):
            if nums[i] >= step:
                step = 1
                continue
            else:
                step += 1
        return step == 1

 

reference:

I almost told difference: my goal - to constantly measure the distance, his goal - changing the destination

class Solution(object):
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if not nums or len(nums)==1 : return True
        des = len(nums) - 1
        for i in range(len(nums)-2,-1,-1):
            if nums[i] >= des - i:
                des = i
        return des == 0 

 

Guess you like

Origin www.cnblogs.com/rosyYY/p/10978792.html