Appearance
Approach: Greedy jump counting: track the farthest reachable in the current jump window; when the index passes the window end, take another jump.
Complexity: O(n) time, O(1) space
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