Python solutions: beat 93.72%


  • 0
    R
    class Solution(object):
    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums);
        if n < 2:
            return 0;
        step = 0;
        lc =0; # current step can reach lc
        ln = nums[0]; # next jump will reach ln at most
        for i in xrange(n):
            if i > lc:  # if index beyond current step, then update lc and step to move forward
                lc = ln;
                step += 1;
            if i + nums[i] > ln: # if i's next jump region beyond ln, update ln then.
                ln = i + nums[i];
            if ln >= n-1: # when ln can reach the end, just one more step will finish the game
                return step + 1;

  • 0
    F

    Your code does not work. Is it complete?


Log in to reply
 

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