Jump Game II

LeetCode

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

You are given an array nums. Each element represents your maximum jump length. Return the minimum number of jumps to reach the last index.

1def jump(nums: List[int]) -> int:
2 jumps = 0
3 end = 0
4 farthest = 0
5
6 for i in range(len(nums) - 1):
7 farthest = max(farthest, i + nums[i])
8 if i == end:
9 jumps += 1
10 end = farthest
11
12 return jumps
i
2
0
3
1
1
2
1
3
4
4
end=0
farthest=2
jumps=0
Step 1 / 3
Step 1:
Scan current jump range [0..end], track farthest for next jump.
Pointers: i=0
Focus: select @ [0]
end=0farthest=2jumps=0