Skip to content

55. Jump Game

https://leetcode.com/problems/jump-game/

js
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function(nums) {
  var reach = 0

  for (let i = 0; i <= reach; i++) {
    reach = Math.max(reach, nums[i] + i)
    if (reach >= nums.length - 1) {
      return true
    }
  }

  return false
}
py
class Solution(object):

    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        reach = 0
        i = 0
        while i <= reach:
            reach = max(reach, nums[i] + i)
            if reach >= len(nums) - 1:
                return True
            i += 1
        return False