LeetCode——Jump Game II
Description:
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.)
贪心
评论区有一个答案Orz:https://leetcode.com/discuss/422/is-there-better-solution-for-jump-game-ii
/*
* We use "last" to keep track of the maximum distance that has been reached
* by using the minimum steps "ret", whereas "curr" is the maximum distance
* that can be reached by using "ret+1" steps. Thus,
* curr = max(i+A[i]) where 0 <= i <= last.
*/
class Solution {
public:
int jump(int A[], int n) {
int ret = 0;
int last = 0;
int curr = 0;
for (int i = 0; i < n; ++i) {
if (i > last) {
last = curr;
++ret;
}
curr = max(curr, i+A[i]);
} return ret;
}
};
LeetCode——Jump Game II的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] Jump Game II(贪婪算法)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode–jump game II
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- leetcode Jump Game II python
@link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【To Read】LeetCode | Jump Game II(转载)
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
随机推荐
- 如何编写一个PHP的C扩展
为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势. 另外C扩 ...
- Windows server 2008 R2充当路由器实现网络的互联(转)
1.路由器的工作原理 当IP子网中的一台主机发送IP分组给同一IP子网的另一台主机时,它将直接把IP分组送到网络上,对方就能收到.而要送给不同IP子网上的主机时,它要 选择一个能到达目的子网上的路由器 ...
- [leetcode]Find Minimum in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...
- Visual Studio 2013 prerequisites
http://www.visualstudio.com/zh-cn/products/visual-studio-ultimate-with-msdn-vs#Fragment_SystemRequir ...
- WPF TextBox自动滚动到最户一行
textBox经常用来显示程序的运行状态或者消息,如何让他自动滚动呢? 在显示消息代码下加一条自动滚动到底部的语句即可: TextBox1.ScrollToEnd(); (如果要显示垂直滚动条设置Ve ...
- 可编辑的DIV -编辑器
找了好多,没几个好用的,都或多或少有问题 目前这个最好用.. 不过有一个奇葩的问题,就是要放在"<a></a>"标签里面, js或者jQuery获取 $ ...
- win8 IIS
IIS打开页面报500错误 aspnet_regiis.exe -i 报 “此操作系统版本不支持此选项” 决解方法: 控制面板 - 程序和功能 - 启动或关闭windows功能 - Internet ...
- Day One studying english
I start study english lately,but the is no basis for english.Only i use baidu translation,google tra ...
- [LeetCode] Remove Invalid Parentheses
This problem can be solved very elegantly using BFS, as in this post. The code is rewritten below in ...
- 节日EDM系列:圣诞节如何进行EDM数据营销
消费关系升级,消费者看中的早已不是产品本身,场景消费以及消费带来的价值感体验已成为影响消费的重要因素.圣诞将至,如何才能将圣诞节EDM数据营销的效果发挥到极致? ① 节日元素创意融合,高辨识度加深品 ...