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.

Example:

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.

Note:

You can assume that you can always reach the last index.

这题是之前那道 Jump Game 的延伸,那题是问能不能到达最后一个数字,而此题只让求到达最后一个位置的最少跳跃数,貌似是默认一定能到达最后位置的? 此题的核心方法是利用贪婪算法 Greedy 的思想来解,想想为什么呢? 为了较快的跳到末尾,想知道每一步能跳的范围,这里贪婪并不是要在能跳的范围中选跳力最远的那个位置,因为这样选下来不一定是最优解,这么一说感觉又有点不像贪婪算法了。其实这里贪的是一个能到达的最远范围,遍历当前跳跃能到的所有位置,然后根据该位置上的跳力来预测下一步能跳到的最远距离,贪出一个最远的范围,一旦当这个范围到达末尾时,当前所用的步数一定是最小步数。需要两个变量 cur 和 pre 分别来保存当前的能到达的最远位置和之前能到达的最远位置,只要 cur 未达到最后一个位置则循环继续,pre 先赋值为 cur 的值,表示上一次循环后能到达的最远位置,如果当前位置i小于等于 pre,说明还是在上一跳能到达的范围内,根据当前位置加跳力来更新 cur,更新 cur 的方法是比较当前的 cur 和 i + A[i] 之中的较大值,如果题目中未说明是否能到达末尾,还可以判断此时 pre 和 cur 是否相等,如果相等说明 cur 没有更新,即无法到达末尾位置,返回 -1,代码如下:

解法一:

class Solution {
public:
int jump(vector<int>& nums) {
int res = , n = nums.size(), i = , cur = ;
while (cur < n - ) {
++res;
int pre = cur;
for (; i <= pre; ++i) {
cur = max(cur, i + nums[i]);
}
if (pre == cur) return -; // May not need this
}
return res;
}
};

还有一种写法,跟上面那解法略有不同,但是本质的思想还是一样的,关于此解法的详细分析可参见网友 实验室小纸贴校外版的博客,这里 cur 是当前能到达的最远位置,last 是上一步能到达的最远位置,遍历数组,首先用 i + nums[i] 更新 cur,这个在上面解法中讲过了,然后判断如果当前位置到达了 last,即上一步能到达的最远位置,说明需要再跳一次了,将 last 赋值为 cur,并且步数 res 自增1,这里小优化一下,判断如果 cur 到达末尾了,直接 break 掉即可,代码如下:

解法二:

class Solution {
public:
int jump(vector<int>& nums) {
int res = , n = nums.size(), last = , cur = ;
for (int i = ; i < n - ; ++i) {
cur = max(cur, i + nums[i]);
if (i == last) {
last = cur;
++res;
if (cur >= n - ) break;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/45

类似题目:

Jump Game

参考资料:

https://leetcode.com/problems/jump-game-ii/

https://leetcode.com/problems/jump-game-ii/discuss/18028/O(n)-BFS-solution

https://leetcode.com/problems/jump-game-ii/discuss/18023/Single-loop-simple-java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 45. Jump Game II 跳跃游戏之二的更多相关文章

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

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

  2. [LeetCode] 45. Jump Game II 跳跃游戏 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 ☆☆☆☆☆(跳跃游戏 2)

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

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

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

  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 解题思路

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

  8. [LeetCode] Flip Game II 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

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

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

随机推荐

  1. PHP 中的关于 trait 的简单

    什么是 trait 看看 PHP 官网的介绍. 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.T ...

  2. css样式重置 移动端适配

    css  默认样式重置 @charset "utf-8"; *{margin:0;padding:0;} img {border:none; display:block;} em, ...

  3. border和outline的区别

    如果有一个需求,给一个元素增加一条边框,想必大家会习惯且娴熟的使用border来实现,我也是这样   但其实outline也能达到同样的效果,并且在有些场景下会更适用,比如下面的demo 使用bord ...

  4. Srinath总结 架构师们遵循的 30 条设计原则

    作者:Srinath 翻译:贺卓凡,来源:公众号 ImportSource Srinath 通过不懈的努力最终总结出了 30 条架构原则,他主张架构师的角色应该由开发团队本身去扮演,而不是专门有个架构 ...

  5. Django学习笔记(15)——中间件

    当Django处理一个Request的过程是首先通过中间件,然后再通过默认的URL方式进行的.我们可以在Middleware这个地方把所有Request拦截住,用我们自己的方式完成处理以后直接返回Re ...

  6. Python term 模块

    Python term 模块 term 模块适用于文字编辑,样式切换,光标移动 等一系列操作适用于终端命令行文字光标处理等操作. 安装 pip install py-term 使用方式 方式一 imp ...

  7. redis 面试问题问答Top 10

    1)什么是Redis? English:Redis is an open source (BSD licensed), in-memory data structure store, used as ...

  8. SQLi-LABS Page-2 (Adv Injections) Less30-Less35

    Less-30 GET - BLIND - IMPIDENCE MISMATCH- Having http://10.10.202.112/sqli/Less-30?id=1" #false ...

  9. 【设计模式】Factory

    前言 这篇博客将包括两种设计模式,一种是工厂模式,一种是抽象工厂模式.不管是哪一种设计模式,本质上在解决的问题是对象创建的问题.工厂,可以是简简单单的一个函数,也可以是一个有具体实现的类,也可以是一个 ...

  10. odoo12 权限配置1

    权限配置文档说明,这里使用公司开发的两个权限配置模块,可以快速的帮助你来配置复杂的odoo权限. 安装以下两个模块,SystemGroups模块是快速帮助你批量添加,创建基础群组需要用到的模块 Bas ...