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.)

Hide Tags

Array Greedy

 

 
    这题是前面一题的变种,其实用的是贪心算法,不过数据设置了时间限制,所以需要提交效率,最好就是一次O(n)时间。
    为了提高时间需要做一个思考,如下面位置,是否存在 i-th 需要跳的最少次数少于i-1 th 位置,如
... i-1 i
... 3 2

  这个可以这么思考,如果i 是从i-1 跳到的,那么i位置的最少跳数 <i> = <i-1> +1.

    如果i 是从i-2  或之前跳到的,那么<i> = <i-1>.
    可以知道必定有<i>   >=   <i-1>。
    利用这个性质可以加快速度。
算法逻辑:
  1. 创建等长的字符串a,a[i] 表示到i-th  的最少跳数,有a[0]等于0,结果就是a[n-1]。
  2. 创建最大已知位置索引maxIdx,表示在i-th 位置时,已经最远确定的位置索引,初始化为1,即指向未知的位置最小的。
  3. 遍历数组。
  4. 如果遍历位置加上跳跃长度大于等于maxIdx,那么更新数组a 到最大索引maxIdx
  5. 如果maxIdx ==n 跳出
  6. 返回a[n-1].

这样遍历的时间其实只有n 次,时间O(n),空间也是O(n).改进地方就是空间改O(1)。

 #include <iostream>
using namespace std; class Solution {
public:
int jump(int A[], int n) {
int *a = new int [n];
for(int i=;i<n;i++)
a[i]=INT_MAX;
a[]=;
int maxidx = ;
for(int i=;i<n;i++){
while(maxidx<=i+A[i]&&maxidx<n){
// for(int kk=0;kk<n;kk++) cout<<a[kk]<<" ";
// cout<<endl;
a[maxidx++] = a[i]+;
}
}
int ret = a[n-];
delete []a;
return ret;
}
}; int main()
{
int a[]={,,,,};
Solution sol;
cout<<sol.jump(a,sizeof(a)/sizeof(int))<<endl;
return ;
}
discuss 中有空间O(1)的实现,逻辑是一样的,就贴上来吧。
https://oj.leetcode.com/discuss/422/is-there-better-solution-for-jump-game-ii
 class Solution {
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]);
} 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

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

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

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

  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(贪婪算法)

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

  7. leetcode–jump game II

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

  8. leetcode Jump Game II python

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

  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. 基于flash-marker.js 的地图标注闪烁代码调试

    修改网上流传的flash-marker.js (function (global, factory) { typeof exports === 'object' && typeof m ...

  2. mysql索引详细描述与应用场景

    索引的数据结构: (1)一般是B+tree:MySql使用最频繁的一个索引数据结构,数据结构以平衡树的形式来组织,因为是树型结构,所以更适合用来处理排序,范围查找等功能. (2)Hash:Hsah索引 ...

  3. DFS:Tempter of the Bone (规定时间达到规定地点)

    解题心得: 1.注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到.并不是最简单的dfs 2.在规定时间达到规定的地点有几个剪枝: 一.公式:所需的步骤 - x相差行 - y相差列 = 偶 ...

  4. layer父页面调用子页面的方法

    由于不知道如何在子页面获取到layer定义的确定按钮,于是就在子页面上定义了一个方法,然后在由父页面在点确定按钮时调用子页面所定义的这个方法,从而执行子页面方法里面的内容: 子页面代码: functi ...

  5. 用NPOI完成公司任务(主要就是导入导出操作)

    注意要先添加程序集的引用 导出到excel: public override IWorkbook writeExecl(string filePath, DataTable dt) { if (Fil ...

  6. Android开发——常见的内存泄漏以及解决方案(一)

    0. 前言   转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52333954 Android的内存泄漏是Android开发领域永恒的 ...

  7. Python之print函数详解

    输出的 print 函数总结: 1. 字符串和数值类型可以直接输出 >>> print(1) 1 >>> print("Hello World" ...

  8. Logistic回归python实现小样例

    假设现在有一些点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归.利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,依次进行分类.Lo ...

  9. Java Integer于Int 进行==双等于的内存比较时的一些问题说明

    转自: https://blog.csdn.net/xingkongdeasi/article/details/79618421 部分有所修改: 前言: 越是简单的东西,我们往往越是没有去把它明白,但 ...

  10. 我给女朋友讲编程CSS系列(2)- CSS语法、3大选择器、选择器优先级

    首先看一下使用Css设置h1标签字体颜色和大小的例子,效果图如下: 新建一个网页test.html,然后复制粘贴下面的内容: <html> <head> <style t ...