当前位置:网站首页>leetcode:55. 跳跃游戏【经典贪心】
leetcode:55. 跳跃游戏【经典贪心】
2022-06-24 19:30:00 【白速龙王的回眸】
分析
维护当前走到的位置,以及当前能走的最大区间内下一次走到的最远位置maxPos
当前能走的位置就是[cur, maxPos]但是在走的过程中,边走边看下一次的maxPos,然后当cur走到上一个maxPos时,下一个maxPos也出来
因此这个是走着脚下的,看着前方的贪心
贪心策略就是:我走着当前能走的所有位置,看到下一次能走的最远距离
那我下一次能走的最远距离就是当前区间内的最大的nums[i] + i
最后看看cur能不能到len(nums) - 1即可
ac code
class Solution:
def canJump(self, nums: List[int]) -> bool:
cur, maxPos = 0, 0
# 这一步能走的最大区间
while cur < maxPos + 1:
# 当前的位置,到能走的最远位置
for i in range(cur, maxPos + 1):
# 更新下一个的最远的位置
maxPos = max(maxPos, nums[i] + i)
# 如果一步步走到了最后
if cur == len(nums) - 1:
return True
# cur 和 i相对应
cur += 1
return False
总结
跳跃游戏 跟 车加油游戏差不多
经典贪心
边栏推荐
猜你喜欢
Several schemes of traffic exposure in kubernetes cluster
心楼:华为运动健康的七年筑造之旅
Want to be a test leader, do you know these 6 skills?
[camera Foundation (I)] working principle and overall structure of camera
These map operations in guava have reduced my code by 50%
我国SaaS产业的发展趋势与路径
2022 international women engineers' Day: Dyson design award shows women's design strength
专科出身,2年进苏宁,5年跳阿里,论我是怎么快速晋升的?
Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
Multithreaded finalization
随机推荐
印刷行业的ERP软件的领头羊
Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
You are using pip version 21.1.2; however, version 22.1.2 is available
Multiplexer select
《各行业零代码企业应用案例集锦》正式发布
【吴恩达笔记】卷积神经网络
排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
Stl+ tree
leetcode1720_2021-10-14
leetcode-201_ 2021_ 10_ seventeen
基于kruskal的最小生成树
Datakit 代理实现局域网数据统一汇聚
Maximum flow problem
壹沓科技签约七匹狼,助力「中国男装领导者」数字化转型
Machine learning: gradient descent method
leetcode_ one thousand three hundred and sixty-five
Summary of papers on traveling salesman problem (TSP)
Redis+Caffeine两级缓存,让访问速度纵享丝滑
leetcode_1365
好想送对象一束花呀