题目
Given an array of non-negative integers nums, 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.
You can assume that you can always reach the last index.
解题思路
相当于BFS,每次从上次可以跳到最远的地方到这次可以跳到最远的地方向后遍历,实现的时候只需要记录上次以及当前可以跳到最远的地方,遍历到上次可以跳到最远的地方之后,跳的次数加1
代码
class Solution:
def jump(self, nums: List[int]) -> int:
jump = curEnd = curFar = 0
l = len(nums)
for i in range(l - 1):
curFar = max(curFar, i + nums[i])
if curEnd == i:
jump += 1
curEnd = curFar
return jump