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. ASP.NET Core模块化前后端分离快速开发框架介绍之2、快速创建一个业务模块

    源码地址 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin ...

  2. vue 判断是否登录,未登录跳转到登录页

    网页一进入判断是否登录,未登录跳转到登录页面 router.js export default new Router({ routes: [ { path: '/', name: 'HelloWorl ...

  3. Python9-MySQL索引-外键-day43

    1.以ATM引出DBMS2.MySQL -服务端 -客户端3.通信交流 -授权 -SQL语句 -数据库 create database db1 default charset=utf8; drop d ...

  4. Linux命令之---mv

    命令简介 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files) 命令格式 mv [选项] 源文件或目录 目标文件或目录 命令参数 -b 若需覆盖文件,则覆 ...

  5. linux centos6 系统优化脚本-经典

    转载一篇Ricky的系统优化脚本,这个脚本只能针对centos6x 其他还没有测试,但centos7肯定不行的 #!/bin/bash # ID 201510192126 # Author Ricky ...

  6. 如何拿到半数面试公司Offer——我的Python求职之路(转)

    从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...

  7. sql server 不可见字符处理 总结

    前言 问题描述:在表列里有肉眼不可见字符,导致一些更新或插入失败. 几年前第一次碰见这种问题是在读取考勤机人员信息时碰见的,折腾了一点时间,现在又碰到了还有点新发现就顺便一起记录下. 如下图所示 go ...

  8. github readme.md 添加图片

    简要: 将图片放在仓库里面,在文件里链接它,最后 push 到 github 上. github 图片链接格式: (http://github.com/yourname/your-repository ...

  9. 聊聊、Java 命令 第三篇

    这篇随笔主要写启动 jar 时,如果需要依赖其他的 jar 包该怎么处理,我会以 rabbitMQ 客服端启动为例. package com.rockcode.www.rabbitmq; import ...

  10. javascript计算两个时间的差

    function GetDateDiff(startTime, endTime, diffType) { //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 startTime ...