题目

解法
对于位置i来说,假设目前我们能到达的最远边是cur_right,为了能够到达最远方,我们肯定需要在在i到cur_right中选择一个位置进行建桥;注意,无论选择哪个位置,那么从该位置到针对该位置的最远桥next_right的之间位置都是到达;因此,只要每次选择位置的时候,我们都选择next_right最远的那个就可以了;
我们不需要考虑具体选择哪个,我们只要维护好最远next_right的就行,以及记录建桥次数。
cpp
class Solution:
def jump(self, nums: List[int]) -> int:
ans = 0
cur_right = 0#记录当前已经建造的桥的最远右边举例
next_right = 0
for i in range(len(nums)-1):
next_right = max(next_right, nums[i] + i)#记录可以建造的最远的桥
if i == cur_right:
cur_right = next_right
ans += 1
return ans