Skip to content

45. Jump Game II

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

js
/**
 * @param {number[]} nums
 * @return {number}
 */
var jump = function(nums) {
  var result = 0
  var current = 0
  var last = 0

  for (let i = 0; i < nums.length; i++) {
    if (i > last) {
      last = current
      result++
    }
    current = Math.max(current, nums[i] + i)
  }

  return result
}
py
class Solution(object):

    def jump(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result = 0
        current, last = 0, 0
        for i in range(len(nums)):
            if i > last:
                last = current
                result += 1
            current = max(current, nums[i] + i)
        return result