Easy to understand, beats 98% of python submissions.


  • 0
    M

    This is simple code which counts the number nodes from the start which has maximum reach in the previous node's reach.

    class Solution(object):
        def jump(self, nums):
            stk = []
            mx,stp, endpt,i, cnt = 0, 0, 0, 0, 0
            while i < len(nums)-1:
                reach = i+nums[i]
                if reach >= mx:
                    mx, stp = reach, i
                if i == endpt: i,endpt,cnt = stp, mx,cnt+1
                i+=1
            return cnt
    

    This can be optimized further but it has no effect on the run time.


Log in to reply
 

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