题解 | #跳跃游戏(三)# Python3
跳跃游戏(三)
https://www.nowcoder.com/practice/d92a70f4f42248d688b93c9e50d2e757
import sys # 下一个要跳跃的点,是在当前能跳跃的点里面,能跳最远的 # 因为其他点的话,当前能跳跃的点能到达其他所有点能跳跃的地方 n = int(input()) if n==0: print(-1) elif n == 1: print(0) else: nums = list(map(int,input().strip().split(' '))) boundary = nums[0] max_len = nums[0] time = 0 next_boundary = nums[0] for i in range(1,n): next_boundary = max(nums[i]+i, next_boundary) next_boundary = min(next_boundary, n-1) if i == boundary: boundary = next_boundary time += 1 if boundary < n-1: print(-1) else: print(time)