[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 ...
随机推荐
- 正则表达式学习日记zz
1."."为通配符,表示任何一个字符,例如:"a.c"可以匹配"anc"."abc"."acc": ...
- bzoj1032 [JSOI2007]祖码Zuma
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 672 Solved: 335[Submit][Stat ...
- BZOJ3251 : 树上三角形
BZOJ AC1000题纪念~~~ 将x到y路径上的点权从小到大排序 如果不存在b[i]使得b[i]+b[i+1]>b[i+2]则无解 此时b数列增长速度快于斐波那契数列,当达到50项时就会超过 ...
- 有关g++编译调试的问题
打了个指针版的treap,想用gdb调试,用gcc -g ×××.cpp -o a 时却报错了——直接用gcc编译却不会报错,提示:对‘operator new(unsigned int)’未定义的引 ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- [NOI 2014]做题记录
[NOI2014]起床困难综合症 按位贪心 #include <algorithm> #include <iostream> #include <cstring> ...
- 封装同步的UIActionSheet
封装同步的UIActionSheet 发问题 做 iOS 开发的同学想必都用过 UIActionSheet.UIActionSheet 可以弹出一个选择列表,让用户选择列表中的某一项操作.使用 UIA ...
- UIWebView弹出键盘按钮显示中文
UIWebView是一个很常用的视图,一般用来加载网页,比如百度: 点击文本框输入框后,会弹出一个带有toolbar的键盘,toolbar中有3个辅助按钮 有了这3个按钮,是方便很多,但默认是英文的, ...
- Grasshopper 2.0 MP Color FireWire 1394b (Sony ICX274)
相机参数如下,参见这里: Resolution 1624 x 1224 Frame Rate 30 FPS Megapixels 2.0 MP Chroma Color Sensor Name ...
- jquery限制div字符长度,超出部分已“…”显示
$(".content").each(function(){ if($(this).text().length>100){ $(this).text($(this).text ...