[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 ...
随机推荐
- 算法教程(3)zz
First off, we can use our Line-Point Distance code to test for the "BOUNDARY" case. If the ...
- Travelling
Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- linux eclipse cdt make error 127
不知道为啥,copy原来的eclipse环境到新的地方后,编译总是出错: make:*** error 127 解决方案是:属性Properties---C++编译 c++build---build ...
- MONO 架构
- Http中涉及到的知识点总结
1.URL地址 协议-> HTTP:超文本传输协议,除了用来传输文本,还可以传输HTML页面.CSS文件.JS文件.图片.音视频... HTTPS:SSL,它比HTTP更加安全一些 FTP:文件 ...
- .NET生成静态页面的方案总结
转载自:http://www.cnblogs.com/cuihongyu3503319/archive/2012/12/06/2804233.html 方法一:在服务器上指定aspx网页,生成html ...
- lastLogon和lastLogonTimestamp的区别
如何得到用户最近一次登陆域的时间?在Windows2003域中有2个属性:lastLogon和lastLogonTimestamp,那么这2个属性到底有什么作用呢? lastLogon属性实时更新用户 ...
- 用Editplus开发HTML
准备工作 设置Editplus默认的打开浏览器为系统默认浏览器 取消Editplus的临时缓存文件 ☆ 设置不生成临时文件 这里的临时文件,指的是.bak文件,当你在Editplus下修改任意一个文件 ...
- ProtocalBuffers学习记录
Google Protocol Buffer 的使用和原理 Google Protocol Buffers 概述 Google Protocol Buffers 入门 Protocol Buffers ...
- [学点英语]扎克伯格给女儿的信,translation of zucherber's letter to her daughter( Chinese version)
A letter to our daughter 扎克伯格写给女儿的信 Mark Zuckerberg·Tuesday, December 1, 2015 Dear Max, 亲爱的玛克斯 You ...