[leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
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青蛙跳(跳到终点最小步数)的更多相关文章
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [leetcode] 45. Jump Game II(hard)
原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...
- [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 ...
随机推荐
- PythonStudy——可变与不可变 Variable and immutable
ls = [10, 20, 30] print(id(ls), ls) ls[0] = 100 print(id(ls), ls) print(id(10)) print(id(20)) print( ...
- 一篇讲解如何调试pg 扩展的文章
以下链接这片关于pg 扩展调试的文章挺不错,记录下 http://big-elephants.com/2015-10/writing-postgres-extensions-part-iii/ ...
- 第2章 Java基本语法(上): 变量与运算符
2-1 关键字与保留字 关键字(keyword) 保留字(reserved word) 2-2 标识符(Identifier) 案例 class Test{ public static void ma ...
- Aria2+百度网盘 无限制的下载神器
Aria2是一款免费开源跨平台且不限速的多线程下载软件,Aria2的优点是速度快.体积小.资源占用少:支持 HTTP / FTP / BT / Magnet 磁力链接等类型的文件下载:支持 Win.M ...
- vue 项目全局修改element-ui的样式
引入了element-ui,但是和我们自己的样式颜色有很大的不同, 修改例子:在src文件下创建 element-var.scss,代码如下 $--color-primary: yellow; /* ...
- inception 自动化sql审核
##概念: Inception是一款自动化运维的利器,有别与现在各个公司使用的方式,使用Inception,将会给DBA带来最大的便利性,将DBA从繁冗的工作中解放出来,做一些更多的自动化工作,或者从 ...
- lecune入门示例
注意:本示例中的lucene版本需在jdk7以上使用. 一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- ubuntu server cloud img username password
新安装了OpenStack Queens发现无镜像,蹦蹦跳跳的下载了ubuntu的镜像 网址https://cloud-images.ubuntu.com/ 最好你自己找你想要的,vmdk.ova.i ...
- oninput、onchange与onpropertychange事件的区别, 与input输入框实时检测
这几天项目着急,同时也学到好多以前没有接触过的知识.oninput.onchange与onpropertychange事件的区别, 与input输入框实时检测 onchange事件只在键盘或者鼠标操作 ...
- JVM之堆内存(年经代,老年代)
一.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...