45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
Code:
This problem has a nice BFS structure. Let’s illustrate it using the example nums = [2, 3, 1, 1, 4]
in the problem statement. We are initially at position 0
. Then we can move at most nums[0]
steps from it. So, after one move, we may reach nums[1] = 3
or nums[2] = 1
. So these nodes are reachable in 1
move. From these nodes, we can further move to nums[3] = 1
and nums[4] = 4
. Now you can see that the target nums[4] = 4
is reachable in 2
moves.
Putting these into codes, we keep two pointers start
and end
that record the current range of the starting nodes. Each time after we make a move, update start
to be end + 1
and end
to be the farthest index that can be reached in 1
move from the current [start, end]
.
class Solution:
# @param {integer[]} nums
# @return {integer}
def jump(self, nums):
n, start, end, step = len(nums), 0, 0, 0
while end < n - 1:
step += 1
maxend = end + 1
for i in range(start, end + 1):
if i + nums[i] >= n - 1:
return step
maxend = max(maxend, i + nums[i])
start, end = end + 1, maxend
return step