An intuitive solution in Python


  • 0
    L

    An intuitive solution. Kind of a greedy algorithm in O(n) time.

    class Solution(object):
        def jump(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            start = 0
            end = 0
            count = 0
            
            while start <= end and end < len(nums) - 1:
                count += 1
                curt = end
                for i in range(start, end + 1):
                    if nums[i] + i > end:
                        end = nums[i] + i
                start = curt + 1
            
            return count if end >= len(nums) - 1 else -1
    

Log in to reply
 

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