[LeetCode] 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.)
用queue实现bfs的思想,Memory Limited Exceeded!
然后把queue改成vector实现同样的思想,Time Limited Exceeded!
class Solution {
public:
int jump(int A[], int n) {
if(n<)
return ;
pair<int,int> temp;//pair记录下标和到此下标的步数
vector<pair<int,int> > v1;
int minStep = n ;
temp = make_pair(,);
v1.push_back(temp);
while(!v1.empty()){
temp = v1.back();
int Index = temp.first;
int step = temp.second;
v1.pop_back();
if(Index>=n-){
if(step == )
return ;
else if(step<minStep)
minStep = step;
continue;
}
int newIndex = Index + A[Index];
if(newIndex == Index)
continue;
temp = make_pair(newIndex,++step);
v1.push_back(temp);
for(int i = Index+;i<newIndex;i++){
if(i+A[i]<newIndex)
continue;
else{
temp = make_pair(i+A[i],step+);
v1.push_back(temp);
}
}
}//end while
return minStep;
}//end func
};
克服浪费时间和浪费空间的问题,Here is a solution from other,怎么会如此简洁(贪婪算法):
/*
* 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 { //last和curr都是下标
public:
int jump(int A[], int n) {
int ret = ;
int last = ;
int curr = ;
for (int i = ; i < n; ++i) {
if (i > last) {
last = curr;
++ret;
}
curr = max(curr, i+A[i]); //经过ret+1步跳到的下标curr位置
}
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
Description: 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
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 ...
随机推荐
- 解决ibus图标为红圈(图标丢失)
修正IBUS图标丢失gconftool –type boolean -s /desktop/ibus/panel/show_icon_on_systray truegconftool –type bo ...
- COJ0700 数学(一)
试题描述 现在有一大堆数,请你对这些数进行检验. 输入 第一行:CAS,代表数据组数(不大于500000),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数.你需要对于每个数字检验是 ...
- NodeJS中form上传附件中针对表单的multiple attribute出现的问题总结
在express中上传附件需要在表单中添加enctype="multipart/form-data"属性,并且在新的4.0.1版本中需要手动添加中间件app.use(connect ...
- (function(){})()的用法
最近在整理javascript 学习,发现这个问题了 ,在网上发现这么个解释 最清楚 最明白 : (function(){})() 相当于先定义 function xx(){},后调用 xx(); ( ...
- Winform窗体事件发生顺序
Form 和Control 类公开了一组与应用程序启动和关闭相关联的事件.当Windows 窗体应用程序启动时,主窗体的启动事件按以下顺序引发: System.Windows.Forms.Contro ...
- Ajax注册验证用户名是否存在 ——引自百度经验
Ajax注册验证用户名是否存在 http://jingyan.baidu.com/article/a948d6515fdf870a2dcd2e85.html
- CSS中zoom:1的作用 ,小标签大作用
CSS中zoom:1的作用兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. ...
- Qt 程序和窗口添加图标
Qt项目在打包发布之后都需要有个个性的程序图标和窗口图标,这样会使程序更加美观大方,下面我们分别来看如何给程序和窗口分别添加图标.我们需要两种格式的图片,一种是.ico的,用来给程序添加图标,一种是. ...
- Camera Calibration and 3D Reconstruction
3D RECONSTRUCTION WITH OPENCV AND POINT CLOUD LIBRARY http://stackoverflow.com/questions/19205557/op ...
- 利用Python的三元表达式解决Odoo中工资条中城镇、农村保险的问题
Python中没有像C#中有三元表达式 A?B:C 但在python中可以通过 A if condition else B 的方式来达到同样的效果. 例如 : 1 if True else 0 输出 ...