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. IHttpModule接口

    IHttpModule向实现类提供模块初始化和处置事件. IHttpModule包含兩個方法: public void Init(HttpApplication context);public voi ...

  2. asp.net 使用IHttpModule 做权限检查 登录超时检查(转)

    IHttpModule 权限 检查 登录超时检查 这样就不需要每个页面都做一次检查 也不需要继承任何父类. using System;using System.Collections.Generic; ...

  3. 造一个Badge Service(徽章)的轮子

    什么是Badge Service 细心的读者朋友一定在很多Github的Repo,npm的package页面看到过诸如 的徽章.这些徽章是干什么用的? 大家看到上文中我引用的Badge的左侧,是Dow ...

  4. 关于sql 资源竞争死锁现象

    问题:System.Exception: 事务(进程 ID 321)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品.请重新运行该事务 死锁最深层的原因就是一个:资源竞争 表现 ...

  5. 监听EditText的变化

    http://liangruijun.blog.51cto.com/3061169/729505 之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下 ...

  6. 深入了解shell

    接触linux很久了,但一直没有总线,老是尝鲜,什么都想学,但好多没多没有记住,特的总结了一些基本的东西,查了很多资料,不完善的方面我会慢慢的更新……   操作系统与外部最主要的接口就叫做shell. ...

  7. Java线程:新特征-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

  8. 关于jQuery,$(":button") 中的冒号是什么意思?

    $(":button") 表示匹配所有的按钮.$("input:checked")表示匹配所有选中的被选中元素(复选框.单选框等,不包括select中的opti ...

  9. Oracle OEM建库实例

    OEM是一个图形化的数据库管理员工具.它为数据库管理员提供了一个集中的系统管理工具,同时 它也是一个用来管理.诊断和调试多个数据库的工具,一个用来管理来自多个地点的多个网络节点和服务的工具.该工具可以 ...

  10. npm 好用工具 for 前端

    1. caniuse npm install -g caniuse-cmd