当前位置:网站首页>leetcode:45. 跳跃游戏II
leetcode:45. 跳跃游戏II
2022-08-10 20:24:00 【uncle_ll】
45. 跳跃游戏 II
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/jump-game-ii
给你一个非负整数数组 nums
,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
假设你总是可以到达数组的最后一个位置。
示例 1:
输入: nums = [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
示例 2:
输`入: nums = [2,3,0,1,4]
输出: 2
提示:
- 1 < = n u m s . l e n g t h < = 1 0 4 1 <= nums.length <= 10^4 1<=nums.length<=104
- 0 < = n u m s [ i ] < = 1000 0 <= nums[i] <= 1000 0<=nums[i]<=1000
解法
- 贪心算法:从第一步起跳,然后找涵盖范围内能跳得最远的,然后之后以最远的那个为起点,开始找后续能跳得远的。
代码实现
贪心算法
- 原始实现
python实现
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) <= 1:
return 0
start = 0
end = 0 # 边界下标
step = 0
while end < len(nums)-1:
maxpos = 0
for i in range(start, end+1):
maxpos = max(maxpos, i+nums[i])
start = end # 下一条的起点
end = maxpos # 最远的边界
step += 1
return step
c++实现
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() <= 1) return 0;
int start = 0, end = 0;
int step = 0;
while (end < nums.size()-1) {
int maxpos = 0;
for (int i=start; i <= end; i++) {
maxpos = max(maxpos, i+nums[i]);
}
start = end;
end = maxpos;
step++;
}
return step;
}
};
复杂度分析
- 时间复杂度: O ( N 2 ) O(N^2) O(N2)
- 空间复杂度: O ( 1 ) O(1) O(1)
- 优化
从上面代码观察发现,其实被 while 包含的 for 循环中,i 是从头跑到尾的。
只需要在一次 跳跃 完成时,更新下一次 能跳到最远的距离。
并以此刻作为时机来更新 跳跃 次数。
就可以在一次 for 循环中处理。
python实现
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) <= 1:
return 0
max_position = 0
end = 0
step = 0
for i in range(len(nums)-1):
max_position = max(max_position, i+nums[i])
if i == end:
end = max_position
step += 1
return step
c++实现
class Solution {
public:
int jump(vector<int>& nums) {
if (nums.size() <= 1) return 0;
int end = 0;
int maxPostion = 0;
int step = 0;
for (int i=0; i<nums.size()-1; i++) {
maxPostion = max(maxPostion, i+nums[i]);
if (i == end) {
end = maxPostion;
step++;
}
}
return step;
}
};
复杂度分析
- 时间复杂度: O ( N ) O(N) O(N)
- 空间复杂度: O ( 1 ) O(1) O(1)
参考
边栏推荐
- XML小讲
- Site Architecture Detection & Chrome Plugin for Information Gathering
- Transferrin-modified osthole long-circulating liposomes/PEG-PLGA nanoparticles loaded with notoginsenoside R1 ([email prot
- Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
- 参天生长大模型:昇腾AI如何强壮模型开发与创新之根?
- 验证码倒计时自定义hooks
- 一次由groovy引起的fullGC问题排查
- Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
- @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
- 测试开发【Mock 平台】08 开发:项目管理(四)编辑功能和Component抽离
猜你喜欢
三子棋的设计和代码
七月券商金工精选
CMU博士论文 | 视频多模态学习:探索模型和任务复杂性
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
网络虚拟化
设备管理中数据聚类处理
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
WCF and TCP message communication practice, c # 】 【 realize group chat function
Hangdian Multi-School Seven 1003-Counting Stickmen (Combination Mathematics)
【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
随机推荐
导入FontForge生成字体
铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
详叙c中的分支与循环
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
Apache DolphinScheduler 3.0.0 正式版发布!
线性结构----链表
史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)
每日一R「03」Borrow 语义与引用
nfs挂载服务器,解决挂载后无法更改用户id,无法修改、写文件,文件只读权限Read-only file system等问题
.NET现代应用的产品设计 - DDD实践
“2022零信任神兽方阵”启动调研,欢迎各单位填报信息
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
机器学习|模型评估——AUC
MySQL数据库的主从复制部署详解
Linux服务器安装Redis,详细步骤。
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
哈工大软件构造Lab3(2022)
越折腾越好用的 3 款开源 APP
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes