当前位置:网站首页>leetcode:45. Jumping game II [classic greed]
leetcode:45. Jumping game II [classic greed]
2022-06-24 22:04:00 【Review of the white speed Dragon King】

analysis
We have reached the last ( You can play jump games 1 Judge )
And then we still follow the interval we can walk each time , Record maxPos
Traverse the positions one by one , Walk and look in the area where you can walk to find the next one maxPos
Then if the current position i touch end( That's the last one maxPos The location of , This is the limit we can go )
Now step Must be added 1, And the next one end Is what we currently record maxPos
The most greedy way
ac code
class Solution:
def jump(self, nums: List[int]) -> int:
# You can play jump games 1 To determine whether the last position can be reached
# This jumping game 2 be based on 1 Find the minimum number of arrivals
# Or greedy , Current [cur, maxPos] look forward , Only those who can go the farthest “ come on. ”
maxPos, mustAdd, step = 0, 0, 0
n = len(nums)
# Do not refuel until you reach the place where you must refuel
# The last position is to reach , No need to refuel
for i in range(n - 1):
# Greedy is the farthest position you can see in the current range
maxPos = max(maxPos, nums[i] + i)
# If you go to a place where you must refuel
if i == mustAdd:
# The place that must be refueled next time
# It is the farthest place you can see in the last section
mustAdd = maxPos
step += 1
return step
summary
Go through all the ranges that the current fuel volume can support
Then go to each position and look at it while walking , If you refuel at this position, the next maximum position you can reach
Then find the next interval in this interval maxPos that will do
边栏推荐
- [notes of wuenda] fundamentals of machine learning
- 平衡二叉搜索树
- [notes of Wu Enda] convolutional neural network
- Detailed installation and use of performance test tool wrk
- 我国SaaS产业的发展趋势与路径
- Mysql 通过表明获取字段以及注释
- leetcode:1504. 统计全 1 子矩形的个数
- SAP interface debug setting external breakpoints
- KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
- [featured] how do you design unified login with multiple accounts?
猜你喜欢

基于 KubeSphere 的分级管理实践

Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme

Flutter-使用 typedef的注意事项

【吴恩达笔记】机器学习基础

leetcode-201_ 2021_ 10_ seventeen

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

Opengauss kernel: simple query execution

Datakit 代理实现局域网数据统一汇聚
![[200 opencv routines] 209 Color image segmentation in HSV color space](/img/fa/9a40015cbcf9c78808f147e510be4c.jpg)
[200 opencv routines] 209 Color image segmentation in HSV color space

Several schemes of traffic exposure in kubernetes cluster
随机推荐
leetcode_ 1470_ 2021.10.12
[untitled]
Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
Suspend component and asynchronous component
在每个树行中找最大值[分层遍历之一的扩展]
面试官:你说你精通Redis,你看过持久化的配置吗?
将二维数组方阵顺时针旋转90°
架构实战营 第 6 期 毕业设计
性能测试工具wrk安装使用详解
TCP RTT measurement tips
手动事务的几个类
波卡生态发展不设限的奥义——多维解读平行链
Binary search tree template
Object. Defineproperty and reflect Fault tolerance of defineproperty
suspense组件和异步组件
St Table + two points
权限想要细化到按钮,怎么做?
985 test engineer is hanged. Who is more important in terms of education and experience?
SAP interface debug setting external breakpoints
cv2导包时报Could not find a version that satisfies the requirement cv2 (from versions: none)