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. 简单的shell脚本练习(一)

    1:求1000一内的偶数和 方法一: #!/bin/bash #用累加实现1000以内的偶数和 sum= ;i<=;i=i+)) do sum=$(($sum+$i)); done echo $ ...

  2. 单源最短路径Dijkstra算法,多源最短路径Floyd算法

    1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...

  3. ubuntu18.04安装openresty

    ubuntu18.04使用openresty官方APT源安装openresty 添加openresty的 APT 仓库,这样就可以便于未来安装或更新软件包(通过 apt-get update 命令). ...

  4. 各CF-based tracker中output_sigma_factor取值

    现有的各CF-Based tracker中理想高斯响应中output_sigma_factor的取值情况 默认output_sigma = target_sz*output_sigma_factor; ...

  5. Lattice并购案&我国FPGA发展路径

    FPGA作为通信.航天.军工等领域的关键核心器件,是保障国家战略安全的重要支撑基础.近年来,随着数字化.网络化和智能化的发展,FPGA的应用领域得到快速扩张.美国在FPGA领域拥有绝对的垄断优势,已成 ...

  6. Http User Agent Example

    Browser User Agent  Safari Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603. ...

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

    一.课本五六章知识点总结 1.第五章 继承是一种由已有的类创建新类的机制 子类继承父类的成员变量和方法 子类继承的方法只能操作子类继承和隐藏的成员变量 子类重写或新增的方法只能操作子类继承和新声明的成 ...

  8. linux vue项目+npm run build + nginx

    系统 环境 vue   nginx 步骤 1.打包vue项目 2.配置nginx 打包vue项目 1.项目配置   我们使用服务器的8000端口 2.打包 # npm run build 打包成功会创 ...

  9. day29单例模式的4种实现模式

    单例模式的四种实现模式单例模式实现方式一: import settings class MySQL:  __instance=None  def __init__(self, ip, port):   ...

  10. list.remove的使用分析

    场景描述 在做需求中,有很多情况会出现 对一个list遍历并过滤掉其中特定的数据 这种场景 .但是按照平常的使用方式,发现报错了. public static void main(String[] a ...