Very simple python solution

  • 0
    class Solution(object):
        def canJump(self, nums):
            :type nums: List[int]
            :rtype: bool
            # linear walk updating the number of remaining steps.
            steps = 1
            for v in nums[:-1]:
                steps -= 1
                if v == 0 and steps == 0:
                    return False
                    steps = max(steps, v)
            return True

    This seems easier to read and understand than the editorial solution. Since you are given the opportunity to move up to x steps at each given index, you can just keep a running total of the total number of steps you have remaining, and update them while parsing each element in the list. That means subtracting 1 for each step, then either continuing with the steps you have, or instead using the steps given at that index.

Log in to reply

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