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(贪婪算法)的更多相关文章

  1. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. LeetCode——Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] Jump Game II 贪心

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Leetcode jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. leetcode–jump game II

    1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...

  7. leetcode Jump Game II python

    @link http://www.cnblogs.com/zuoyuan/p/3781953.htmlGiven an array of non-negative integers, you are ...

  8. [Leetcode] jump game ii 跳跃游戏

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. 【To Read】LeetCode | Jump Game II(转载)

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

随机推荐

  1. HDU2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    与POJ2778一样.这题是求长度不超过n且包含至少一个词根的单词总数. 长度不超过n的单词总数记为Sn,长度不超过n不包含词根的单词总数记为Tn. 答案就是,Sn-Tn. Sn=26+262+263 ...

  2. 更新Android SDK之后Eclipse提示ADT版本过低的一个简易解决办法

    首先说明一下发表这一篇博文的“历史原因”吧,因为在更新SDK之后,进入Eclipse设置Android SDK目录的时候,会突然说我的版本低什么的,尝试自己解决但失败之后,我在搜索引擎上找了很多中文的 ...

  3. TYVJ P1001 第K极值 Label:水

    背景 成成第一次模拟赛 第一道 描述 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数 ...

  4. CC150 - 11.5

    Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...

  5. Json 数组拼接

    var str1 = {"name": "apple", "sex": "21"};                 / ...

  6. SVN 中trunk、branches、tags

    SVN 中trunk.branches.tags   我们在一些著名开源项目的版本库中,通常可以看到trunk, branches, tags等三个目录.由于SVN固有的特点,目录在SVN中并没有特别 ...

  7. Scrum会议3(Beta版本)

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  8. OC中属性及方法

    1.声明式属性    a.实例变量    b.声明属性        自动生成setter/getter方法        .h ->@property 属性类型 属性名;        .m ...

  9. 移动Web单页应用开发实践——实现Pull to Request(上/下拉请求操作)

    在单页应用开发中,无论是页面结构化,还是Pull to Request,都离不开一个技术——页面局部滚动.当下的移动web技术,主要使用下面两种方式实现局部区域的滚动: 基于IScroll组件,也有很 ...

  10. JavaScript系列:模块化与链式编程

    模块化:闭包和和函数作用域(JS没有块级作用域ES6之前)构造模块 var man=function(){ var age=12; return { getYear:function(){ retur ...