Solution
-
At each step, we can jump at a range
[l, r]
. l
is where we end up with at the previous step.All positions before it was checked already.
-
Keep the right most position
r
that one can jump withintimes
jump. - When
r == len(nums) - 1
, we have reached our goal.
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) <= 1: return 0
l, r = 0, nums[0]
times = 1
while r < len(nums) - 1:
times += 1
nxt = max(i + nums[i] for i in range(l, r + 1))
l, r = r, nxt
return times
PREVIOUSFirst Missing Positive