当前位置:网站首页>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
总结
跳跃游戏 跟 车加油游戏差不多
经典贪心
边栏推荐
- [untitled]
- 多线程收尾
- 双链表实现
- leetcode1720_ 2021-10-14
- Guava中这些Map的骚操作,让我的代码量减少了50%
- [notes of wuenda] fundamentals of machine learning
- I really can't do it. After 00, I collapsed and wanted to leave
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- 01---两列波在相遇处发生干涉的条件
- leetcode-201_2021_10_17
猜你喜欢

【吴恩达笔记】卷积神经网络

即构「畅直播」上线!提供全链路升级的一站式直播服务

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter

leetcode_191_2021-10-15

Want to be a test leader, do you know these 6 skills?

C语言-关键字1

Practice of hierarchical management based on kubesphere

Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
![[notes of Wu Enda] multivariable linear regression](/img/b1/60a702aaca58b0afa57ac2f552dabf.png)
[notes of Wu Enda] multivariable linear regression

Several schemes of traffic exposure in kubernetes cluster
随机推荐
Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
SAP接口debug设置外部断点
[untitled]
[notes of Wu Enda] convolutional neural network
Guava中这些Map的骚操作,让我的代码量减少了50%
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
TypeScript快速入门
Xinlou: Huawei's seven-year building journey of sports health
Binary search tree template
Several classes of manual transactions
[untitled]
02---纵波不可能产生的现象
Suspend component and asynchronous component
【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
【无标题】
Network layer & IP
Prompt that the device has no permission when using ADB to connect to the device
如何做到全彩户外LED显示屏节能环保
Maximum flow problem