当前位置:网站首页>leetcode:45. 跳跃游戏 II【经典贪心】
leetcode:45. 跳跃游戏 II【经典贪心】
2022-06-24 19:30:00 【白速龙王的回眸】

分析
已经能到达最后一个(可通过跳跃游戏1判断)
然后还是按照每次能走的区间,记录maxPos
一个个位置遍历,在能走的区间内边走边看找到下一个maxPos
然后如果当前位置i触及end(也就是上一个maxPos的位置,这是我们能走的极限)
这时候step必须加1,同时下一个的end就是我们当前记录的maxPos
极尽贪心之道
ac code
class Solution:
def jump(self, nums: List[int]) -> int:
# 可以用跳跃游戏1来判断是否可以到达最后一个位置
# 这个跳跃游戏2基于1求最少到达的次数
# 还是贪心,当前的[cur, maxPos]往前看,能走最远的才“加油”
maxPos, mustAdd, step = 0, 0, 0
n = len(nums)
# 到了必须要加油的地方才加油
# 最后一个位置是要到达,没必要加油
for i in range(n - 1):
# 贪心当前能走区间看到的最远位置
maxPos = max(maxPos, nums[i] + i)
# 如果走到必须加油的地方
if i == mustAdd:
# 下一次必须加油的地方
# 就是上一段区间能看到最远的地方
mustAdd = maxPos
step += 1
return step
总结
走当前油量能支持的所有区间
然后到每个位置边走边看,如果在这个位置加油接下来能到达的最大位置
然后在这个区间内找到下一个区间的maxPos即可
边栏推荐
- 01---两列波在相遇处发生干涉的条件
- Several schemes of traffic exposure in kubernetes cluster
- Junior college background, 2 years in Suning, 5 years in Ali. How can I get promoted quickly?
- The collection of zero code enterprise application cases in various industries was officially released
- 【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
- Several classes of manual transactions
- 降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
- CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
- TKKC round#3
- How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
猜你喜欢
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network

降低pip到指定版本(通過PyCharm昇級pip,在降低到原來版本)

(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model

cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)
![[notes of wuenda] fundamentals of machine learning](/img/71/6192a75446fa7f79469a5483ececc6.jpg)
[notes of wuenda] fundamentals of machine learning

The collection of zero code enterprise application cases in various industries was officially released

想当测试Leader,这6项技能你会吗?

SAP接口debug设置外部断点

Binary search tree template

降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
随机推荐
linq查询集合类入门 案例武林高手类
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
EasyBypass
Yida technology signed a contract with seven wolves to help the digital transformation of "Chinese men's wear leader"
MySQL gets fields and comments by indicating
The process from troubleshooting to problem solving: the browser suddenly failed to access the web page, error code: 0x80004005, and the final positioning: "when the computer turns on the hotspot, the
旅行商问题(TSP)的相关论文总结
[notes of wuenda] fundamentals of machine learning
Guava中这些Map的骚操作,让我的代码量减少了50%
LINQ query collection class introductory cases Wulin expert class
TKKC round#3
leetcode1720_ 2021-10-14
Xinlou: Huawei's seven-year building journey of sports health
Getting started with typescript
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
基于 KubeSphere 的分级管理实践
Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?
在每个树行中找最大值[分层遍历之一的扩展]
降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)