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. ASP.NET中如何实现负载均衡

    ASP.NET站点中做负载均衡: 基于HTTP协议我们可能发现我们要解决两点问题: 第一,做到负载均衡,我们需要一个负载均衡器. 可以通过DNS轮询来做,在DNS服务器上配置为每次对我们做负载均衡的同 ...

  2. iOS UIKit:animation

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

  3. Xcode4快速Doxygen文档注释 — 简明图文教程

    转自:http://blog.csdn.net/totogo2010/article/details/9100767 准备2个文件: 文件一,ThisService.app 文件二,Doxygen.r ...

  4. java web 中的转发和重定向

    假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...

  5. html复选框多行排列布局

    前言:写这篇文章,主要是在于总结一下自己的心得体会... 公司的产品需要实现操作权限配置,我们实现的方式是,左边是“产品”=>“模块”树,右边是由“菜单”和“按钮”复选框按钮.如下图:

  6. 【html】【3】html标签列表

    必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...

  7. 倒置字符串s中各字符的位置

    倒置字符串s中各字符的位置 其中reverse函数可以写成更紧凑的形式 void reverse(char s[]){ int c,i,j; ,j=strlen(s)-;i<j;i++,j--) ...

  8. SGU Volume 1

    SGU 解题报告(持续更新中...Ctrl+A可看题目类型): SGU101.Domino(多米诺骨牌)------------★★★type:图 SGU102.Coprimes(互质的数) SGU1 ...

  9. Linux 特殊权限位

    特殊权限位 LINUX 基本权限有9位但是还有三位特殊权限. suid s(有x权限) S(没有x权限) 4 在用户权限的第三位 sgid s(有x权限) S(没有x权限) 2 在用户组权限的第三位 ...

  10. mount的艺术

    在阅读本文之前,我假设你已经对Linux系统下的硬盘.光盘的设备命令规则有所了解,比如sda和sda1的关系,以及hda.sda.fd.cdrom等设备. === 1 我把U盘插到USB口上了,下一步 ...