Skip to content

Jump Game II

LeetCode

01 · Question

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

02 · Solution

Reference solution

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