题解 | #跳跃游戏(三)# 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)




全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务