当前位置:网站首页>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
总结
跳跃游戏 跟 车加油游戏差不多
经典贪心
边栏推荐
- Stl+ tree
- Implementation of heap sort and quick sort principle
- 应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
- You are using pip version 21.1.2; however, version 22.1.2 is available
- [featured] how do you design unified login with multiple accounts?
- suspense组件和异步组件
- PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘
- 滤波数据分析
- socket(1)
- leetcode_1365
猜你喜欢

Vscode netless environment rapid migration development environment (VIP collection version)

I really want to send a bunch of flowers

leetcode_ 191_ 2021-10-15

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

You are using pip version 21.1.2; however, version 22.1.2 is available

【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index

Machine learning: linear regression

Multiplexer select

socket(1)

Junior college background, 2 years in Suning, 5 years in Ali. How can I get promoted quickly?
随机推荐
Summary of papers on traveling salesman problem (TSP)
985 test engineer is hanged. Who is more important in terms of education and experience?
TCP RTT measurement tips
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
Kubernetes 集群中流量暴露的几种方案
(to be added) games101 job 7 improvement - knowledge you need to know to realize micro surface model
01--- conditions for interference of two trains of waves at the meeting place
Drag drag drag
I really want to send a bunch of flowers
Excel布局
[camera Foundation (I)] working principle and overall structure of camera
【吴恩达笔记】机器学习基础
You are using pip version 21.1.2; however, version 22.1.2 is available
心楼:华为运动健康的七年筑造之旅
平衡二叉搜索树
Datakit 代理实现局域网数据统一汇聚
How to achieve energy conservation and environmental protection of full-color outdoor LED display
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
Data link layer & some other protocols or technologies
These map operations in guava have reduced my code by 50%