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.

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.

题意:

跟之前[leetcode]55. Jump Game青蛙跳(能否跳到终点)思路一致。

不同的是,

要求计算最小步数。

思路:

一、初始化lastReachIdx, 指针i, lastReachIdx, result都为0

二、更新curReachIdx(即当前i的势力范围)

三、指针i继续扫数组,当前指针i若大于lastReachIdx

四、则更新lastReachIdx, 同时result ++。 而curReachIdx更新为当前i对应的势力范围

五、指针i继续扫数组,当前指针i 若大于lastReachIdx,

则说明又要更新lastReachIdx并且result ++了。

代码:

 public class JumpGameII {
public int jump(int[] nums) {
int result = 0;
int lastMaxIdx = 0;
int curMaxIdx = 0;
for (int i = 0; i < nums.length; ++i) {
if (i > lastMaxIdx) {
lastMaxIdx = curMaxIdx;
++result;
}
curMaxIdx = Math.max(curMaxIdx, i + nums[i]);
}
return result;
}
}

[leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)的更多相关文章

  1. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  2. [LeetCode] 45. Jump Game II 跳跃游戏之二

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

  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 跳跃游戏 II

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

  5. [LeetCode] 45. Jump Game II 解题思路

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

  6. Leetcode 45. Jump Game II

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

  7. LeetCode 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  8. [leetcode] 45. Jump Game II(hard)

    原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...

  9. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

随机推荐

  1. ajax 调用webservice 跨域问题

    注意两点 1. 在webservice的config中加入这段位置 (注意不是调用webservice的webconfig中加入) <system.webServer>     <! ...

  2. MySQL Event--Event and EventScheduler

    MySQL Event 创建EVENT语法: CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] event_name O ...

  3. 1.1.20 Word不能保存问题

    1.进入如下目录:C:\用户(user)\Administrator\AppData\Roaming\Microsoft\Templates 2.找到Normal和NormalOld的两个文件,删除. ...

  4. AR外包,就找北京动点软件,长年承接AR外包(案例丰富可签合同,内附案例演示)

    北京团队长年承接AR项目外包 咨询QQ:372900288  微信:liuxiang0884 以下是AR项目案例演示,索取更多案例请通过以上方式在线联系我们

  5. linux系统常用的基本命令分类

    linux系统常用的基本命令分类: 文件命令:vim vimdiff diff mkdir touch rm mv cp ln cd ls more less head tail cat grep e ...

  6. linux git 保存用户名和密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  7. 20165312 2017-2018-2《JAVA程序设计》第8周学习总结

    20165312 2017-2018-2<JAVA程序设计>第8周学习总结 一.第十二章知识点总结 进程与线程 进程是程序的一次动态执行进程,它对应了从代码加载.执行至执行完毕的一个完整过 ...

  8. SpringMVC 启动流程

    首先看一下Web应用部署初始化过程 (Web Application Deployement),官方文档说明: Web Application Deployment When a web applic ...

  9. 编程语言分类,安装python解释器,变量

    1.编程语言分类 机器语言:直接使用二进制指令去编写程序,直接操作硬件 优点:执行效率高 缺点:开发效率低 汇编语言:用英文标签取代二进制指令去编写程序,直接进操作硬件 优点:开发效率高于机器语言 缺 ...

  10. halcon批量读取图片

    以前这个代码都是自己写,不仅繁琐,而且容易忘记.其实Halcon中提供了相关的方法.记录一下吧,其实很简单. 读取一个文件夹下的所有图片[助手]>[打开新的image acquisition ] ...