45. Jump Game II
原理来挺简单的,就是把每一步可以达到的最远距离计算出来,有点像dynamic window的题目,只是left boundary是固定的 lastend + 1,right boundary是上一步能跳到的最大长度,不停的更新就可以了。
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# get maximum jump in each jump
if not nums or len(nums) == 1:
return 0
start = 0
end = start + nums[0]
count = 1
while end < len(nums)-1:
new_end = end
for i in range(start, end+1):
new_end = max(new_end, i + nums[i])
start = end + 1
end = new_end
count += 1
return count