1 题目

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

接口: public int jump(int[] A);

2 思路

从数字的0位置,开始往后挑,求能够达到最后一个元素的最小跳数。

贪心思想:和Jump Game类似,

maxReach表示能够走到的最远的index,不断更新这个全局最值。所以,有局部最值

局部最值: maxLocal = A[i] + i,表示走到i层时,能够达到的最远的index。

step: 走到当前的最小步数

lastReach: 关键变量的引入,在0——lastReach之间的层数,都不会增加step的步数。如何更新这个值成了问题的关键—— 遍历的层数i > lastReach的时候。

复杂度: Time:O(n) Space: O(1)

3 代码

	public int jump(int[] A) {
int len = A.length;
int step = 0, lastReach = 0, maxReach = 0;
for (int i = 0; (i <= maxReach) && (i < len); i++) {
int localReach = A[i] + i;
if (i > lastReach) {
step++;
lastReach = maxReach;
}
maxReach = maxReach > localReach ? maxReach : localReach;
}
return maxReach < len - 1 ? 0 : step;
}

4 总结

贪心的思想,从Jump Game得到思路:采用局部最值和全局最值。同时增加2个变量来获取最终的结果是关键。

5 扩展

Jump Game

6 参考

leetcode面试准备: Jump Game II的更多相关文章

  1. leetcode面试准备: Jump Game

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

  2. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  3. 【LeetCode】45. Jump Game II

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

  4. 【一天一道LeetCode】#45. Jump Game II

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

  5. 【LeetCode】45. Jump Game II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  6. LeetCode OJ 45. Jump Game II

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

  7. [leetcode greedy]45. Jump Game II

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

  8. LeetCode OJ:Jump Game II(跳跃游戏2)

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

  9. 【一天一道LeetCode】#55. Jump Game

    一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...

随机推荐

  1. 15个Linux Yum命令实例--安装/卸载/更新

    在linux环境中, 安装, 卸载更新包是一个极为常见的操作.大部分的linux环境都提供了包的管理工具, 例如:apt-get, dpkg, rpm, yum等等. 一些Linux环境中,yum是默 ...

  2. DLL Export 报错

    编译报错: error : syntax error at token xxxx 修改非Unicode 系统区域设计即可

  3. sql中对查询出来的数据进行分页

    当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...

  4. android中ListView控件

    今天学习了ListView控件和页面跳转,下面大致介绍下: 第一步:创建显示内容的文件vlist.xml: <?xml version="1.0" encoding=&quo ...

  5. adb shell dumpsys package 查看versionCode

    adb shell dumpsys package +包名 输出可以查看包名 aapt dump xmltree xxx.apk AndroidManifest.xml 查看AndroidManife ...

  6. Error Domain=com.google.greenhouse Code=-102

    *** Terminating app due to uncaught exception 'com.google.greenhouse', reason: 'Error Domain=com.goo ...

  7. Nodejs异步流程控制Async

    http://www.cnblogs.com/huair_12/p/4117351.html 很好的总结 关联下 以便以后学习使用

  8. win7下装ubuntu

    需要的东西有: 1,ubuntu系统镜像,下载地址:http://www.ubuntu.com/download/desktop 选64位吧,兼容性好些. 2,空闲的大于20G硬盘空间,这个大小根据个 ...

  9. jquery技巧(持续更新。。)

    (1)集合处理功能         //为索引为0,1,2的元素分别设置不同的字体颜色         $('p').each(function(i){               this.styl ...

  10. nginx 基础文档

    Nginx基础 1.  nginx安装 2.  nginx 编译参数详解 3.  nginx安装配置+清缓存模块安装 4.  nginx+PHP 5.5 5.  nginx配置虚拟主机 6.  ngi ...