[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.当前步数 ...
随机推荐
- AssertValid函数学习
转自http://tsitao.blog.163.com/blog/static/29795822006914105840496/ VC的调试中,AssertValid和Dump函数的应用 CObje ...
- atitit.提升研发效率的利器---重型框架与类库的差别与设计原则
atitit.提升研发效率的利器---重型框架与类库的差别与设计原则 1. 框架的意义---设计的复用 1 1.1. 重型框架就是it界的重武器. 1 2. 框架 VS. 库 可视化图形化 1 2.1 ...
- tomcat安全配置之禁用Directory Listing
什么是Directory Listing?通俗点讲,就是在webapp的目录下如果没有放置index.html或者类似的文件,如果从IE或者其它浏览器文章这个路径时,会惊喜的发现这个目录下的文件列表被 ...
- 使用Intent实现Activity的显式跳转
[正文] 这里以按钮实现活动跳转为例,为实现这个功能,我们需要三个步骤: 1.点击按钮才发生页面跳转,因此,第一步我们先要找到要点击的按钮 如何拿到按钮对象呢?通过资源id,前面我们提到过,在R.id ...
- Android四大组件——Activity
Activity作为Android四大组件之一,也是其中最重要的一个组件.作为一个与用户交互的组件,我们可以把Activity比较成为windows系统上的一个文件夹窗口,是一个与用户交互的界面.再进 ...
- HTML5和CSS3实例教程[总结一]
关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...
- js接收复选框的值
<td><input type="checkbox" class="title" name="title" value=& ...
- WCF调用问题
1---WCF常用的节点绑定 basicHttpBinding和netTcpBinding 当通过Spring对象注入 如 WCFForOrderService 对象注入 public WCFForO ...
- android 检查网络是否可用,如果不可用弹出设置,让用户改变
/** * 校验网络,如果没有网络,返回true * * @return boolean */ @Override public boolean hasInternetConnected() { Co ...
- 让 IE 支持HTML5 placeholder
HTML5 新增的placeholder属性已经得到现代浏览器的支持,旨在提供简单的API可以为文本输入框设置 描述输入字段预期值的提示信息(hint). 这是W3C在标准化的过程中对用户体验的更多考 ...