LeetCode45. 跳跃游戏 II
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt, s, e, far = 0, 0, 0, 0
while far < len(nums) - 1:
cnt += 1
for i in range(s, e + 1):
far = max(far, i + nums[i]) #第cnt步可到达的最远位置
s = e + 1
e = far
return cnt
# test
if __name__ == '__main__':
r = [5,6,4,4,6,9,4,4,7,4,4,8,2,6,8,1,5,9,6,5,2,7,9,7,9,6,9,4,1,6,8,8,4,4,2,0,3,8,5]
s = Solution()
print(s.jump(r))