4 lines Python. O(n)

  • 0

    The trick is to start from the last index doing a greedy approach. Use a counter variable to keep track of the furthest left index that can reach the last index.

        def canJump(self, nums):
            counter = len(nums)-1
            for i in xrange(counter, -1, -1):
                if counter-i <= nums[i]: counter = i
            return counter == 0

Log in to reply

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