[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——元组 Tuple
元组类型 元组:可以理解为不可变的列表1.值可以为任意类型2.可以存放多个值 - 可以进行成员运算3.可以存放重复的值 - 可以计算成员出现的次数4.有序存储 - 可以通过索引取值,可以切片 常用操作 ...
- springboot解决跨域问题
在启动类里加入corsFilter import org.springframework.boot.SpringApplication; import org.springframework.boot ...
- Docker常用命令(四)
通过一些例子来了解基本的命令使用 1.查看docker信息 docker info 2.安装完Docker后,里面还有任何镜像,先从仓库下载一个基础镜像,然后在这个基础 ...
- RabbitMQ 死信队列 延时
package com.hs.services.config; import java.util.HashMap; import java.util.Map; import org.springfra ...
- 文件处理,三元操作符,seek()函数,迭代函数和列表解析,reduce函数
1.文件读取方类型 r,r+,w,x,a, r,读文件 w,写文件,文件内容全部删除,并将新内容从第一行开始赋值 x,写文件,只有文件不存在,可写,文件存在,报错 a,在文件莫问追加信息 r+,w+, ...
- git push 和 pull 时 免密执行的方法
问题:在使用git代码仓库时,使用git clone 获取代码时,如果使用的是https协议,则在每次push时需要输入账号密码.相关文档:文档一,文档二 验证了文档一种的方法二可用,记录一下创建文件 ...
- 知识点:Mysql 基本用法之视图
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- 知识点:synchronized 原理分析
synchronized 原理分析 1. synchronized 介绍 在并发程序中,这个关键字可能是出现频率最高的一个字段,他可以避免多线程中的安全问题,对代码进行同步.同步的方式其实就是隐式的加 ...
- 数据访问安全--数据库遮罩及断词 Data Masking & Tokenization
现在大数据时代几乎无隐私,各政府部门各公司都要求实名制(动不动手机认证,身份证号码认证),但又无力确保数据安全,称为乱象. 其实在2011年,我们就接触过数据库遮罩断词产品,一个澳大利亚公司产品. 简 ...
- solr字段压缩属性compressed新版本已经移除
solr字段压缩属性compressed新版本已经移除 可能是考虑到压缩意义不大还减少搜索效率,所以去掉了.而且好像没有替代属性.