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. AndroidStudio怎么将开源项目发布到jcenter

    前言 自己在网上搜了一大堆,大体就两种方法,而我选择的是其中代码少的的方法,不过他们或多或少留下了少许的坑,(按他们的方法我是上传成功,但不能发布到jCenter上去,也可能是我自己的问题o(≧v≦) ...

  2. scp文件到远端机器问题总结及解决方法

    今天在download服务器日志时遇到了很多问题, 顺便把相应的解决步骤记录下方便以后查看. #把文件copy到192.168.1.102的服务器上 scp -r local_dir readonly ...

  3. 使用Cache防止多人同时修改同一条信息

    Default.aspx: <a href="Default2.aspx?id=123&type=11ad">打开第二个页面id=123</a>&l ...

  4. idea集成svn插件

    1.需要在机器上安装一个SVN客户端命令行程序,可以到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows 我选择的是torto ...

  5. js -去掉首尾的空格.

    function trimFE (str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }

  6. Top 12 Best Free Network Monitoring Tools (12种免费网络监控工具)

    1) Fiddler Fiddler(几乎)是适用于任何平台和任何操作系统的最好的免费网络工具,并提供了一些广受欢迎的关键特性.如:性能测试.捕捉记录HTTP/HTTPs请求响应.进行web调试等很多 ...

  7. [转载] CMake Official Tutorial——教程还是官方的好

    CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Poin ...

  8. 如何给html元素的onclick事件传递参数(即如何获取html标签的data-*属性)

    现在做的一个小系统为了达到领导所说的很炫的效果有用到Metro UI CSS,但是因为如何给每个磁贴(div标签)的click事件传递参数折腾了蛮久(偶是菜鸟),后来终于找到一个解决方案即通过data ...

  9. PHP常用数组函数

      一.数组操作的基本函数 数组的键名和值 array_values($arr);  获得数组的值 array_keys($arr);  获得数组的键名 array_flip($arr);  数组中的 ...

  10. ubuntu 之旅 —— eclipse没有菜单栏

    1. 新建一个eclipse.sh文件,加入如下内容,下面的路径是elcipse的路径 export UBUNTU_MENUPROXY=0 /home/wangdeshui/eclipse/eclip ...