[LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
问题:给定一个数组,每个元素表示你站在该位置可以跳的最大距离。假设你站在第一个元素,求你可以跳到最后元素的最小跳跃次数。
题目简洁,解题却不容易。
数组中的元素值是代表一个范围区间,题目需要求的是最小跳跃次数,也就是每一次的跳跃覆盖的范围应该尽可能远,这是一个大致思路。
- 假设 [ start, end ] 表示第 i 次跳跃才能到达的区间,nextEnd 代表在该区间中起跳的下一个最远元素,那么,[ end+1, nextEnd ] 表示第 i+1 次跳才能去到的范围区间。
- 初始化 [start , end] 为 [0,0],重复执行上面操作,直到 [start, end] 覆盖到终点元素。由于 [start, end] 表示第 i 次跳跃才能到达的区间,所以 i 便是最小的跳跃次数。
在代码实现中, start 变量没有影响到程序的执行,加进去只是为了方便理解。
这个解题思路我没能想到,是参考了 LeetCode 论坛才理解到 : 10-lines C++ (16ms) / Python BFS Solutions with Explanations、Single loop simple java solution
int jump(vector<int>& nums) {
int start = ;
int end = ;
int cnt = ;
int nextEnd = ;
for (int i = ; i < nums.size()-; i++) {
if (i > end) {
return -;
}
nextEnd = max(nextEnd, i + nums[i]);
if (i == end) {
start = i + ;
end = nextEnd;
cnt++;
}
}
return cnt;
}
一开始使用了 DP 解法,已经参照了 LeetCode 的另一个思路,不过时间复杂度都是 O(n*n),都超时。
[LeetCode] 45. Jump Game II 解题思路的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】45. Jump Game II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [leetcode] 45. Jump Game II(hard)
原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...
随机推荐
- ceph主要数据结构解析2-Rados.h文件
(1)文件系统id结构:16个字符组成 struct ceph_fsid { unsigned char fsid[16]; }; 以及对应的比较函数: static inline int ceph_ ...
- 安卓(android)建立项目时失败,出现Android Manifest.xml file missing几种解决方法?(总结中)
安卓(android)建立项目时失败.出现AndroidManifest.xml file missing几种解决方法?(总结中) Eclipse新建项目.遇到这种问题.注意例如以下: 1.文件名称最 ...
- android动画效果编程基础--Android Animation
动画效果编程基础--Android Animation 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 tran ...
- http to https automatic--weblogic/jboss/tomcat--reference
weblogic reference from:http://middlewaremagic.com/weblogic/?p=2019 Many times we want to secure our ...
- INFORMATION_SCHEMA.COLUMNS 查询表字段语句
INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.syscolumns.syscomments.sysconf ...
- 全球最流行的66款App的共同规律
根据苹果AppStore.Google Play.App Annie.亚马逊 AppStore及Windows Phone 应用商店历年的公开数据统计,以下66个非游戏类应用正在全球范围内流行,持续时 ...
- Vim的多窗口模式管理
Vim中的多窗口打开 vim中,默认的多窗口打开,是横向分割窗口. 进入vim编辑器以后,可以通过new命令,新建一个子窗口 :new “新建一个未命名窗口 :new name "新建一个 ...
- C# SqlHelper
操作数据库时,经常会把常用的方法封装到一个类中,这里简单写了一个SQLHelper类,供我平时调用. public static class SqlHelper { private static re ...
- java的练习
import java.awt.GridLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.s ...
- solr和mongodb比较
solr非常灵活,虽然mongodb添加索引查询速度比较快,但是solr查询比mongodb更加灵活,所以需要获取mongodb的oplog,实时将oplog中的数据推送到solr中 oplog A ...