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.

For example:
Given array A = [2,3,1,1,4]

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.)

问题:给定一个数组,每个元素表示你站在该位置可以跳的最大距离。假设你站在第一个元素,求你可以跳到最后元素的最小跳跃次数。

题目简洁,解题却不容易。

数组中的元素值是代表一个范围区间,题目需要求的是最小跳跃次数,也就是每一次的跳跃覆盖的范围应该尽可能远,这是一个大致思路。

  • 假设 [ start, end ] 表示第 i 次跳跃才能到达的区间,nextEnd 代表在该区间中起跳的下一个最远元素,那么,[ end+1, nextEnd ] 表示第 i+1 次跳才能去到的范围区间。
  • 初始化 [start , end] 为 [0,0],重复执行上面操作,直到 [start, end] 覆盖到终点元素。由于 [start, end] 表示第 i  次跳跃才能到达的区间,所以 i 便是最小的跳跃次数。

在代码实现中, start  变量没有影响到程序的执行,加进去只是为了方便理解。

这个解题思路我没能想到,是参考了 LeetCode 论坛才理解到 : 10-lines C++ (16ms) / Python BFS Solutions with ExplanationsSingle loop simple java solution

     int jump(vector<int>& nums) {

         int start = ;
int end = ; int cnt = ; int nextEnd = ; for (int i = ; i < nums.size()-; i++) { if (i > end) {
return -;
} nextEnd = max(nextEnd, i + nums[i]);
if (i == end) {
start = i + ;
end = nextEnd;
cnt++;
}
} return cnt;
}

一开始使用了 DP 解法,已经参照了 LeetCode 的另一个思路,不过时间复杂度都是 O(n*n),都超时。

[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 (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  3. Leetcode 45. Jump Game II

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

  4. [leetcode]45. Jump Game 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 跳跃游戏 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 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...

  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 45 Jump Game II(按照数组进行移动)

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

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

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

随机推荐

  1. poj 3046 Ant Counting (DP多重背包变形)

    题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...

  2. 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择

    一.分析  对于一个字符串进行拼接有三种方法:加号.concat方法.及StringBuiler或StringBuffer. 1."+"方法拼接字符串  str += " ...

  3. StarUML中时序图添加小人

    转载于 http://blog.csdn.net/longyuhome/article/details/9011629 在看时序图的例子的时候,发现有些的时序图上有小人的图标,可是一些UML工具却没有 ...

  4. Java基础知识强化77:正则表达式之获取功能(Pattern 和 Matcher类的使用)

    1. 获取功能: Pattern 和 Matcher类结合使用 2. 使用案例: package cn.itcast_05; import java.util.regex.Matcher; impor ...

  5. sourceTree添加git密钥步骤

    给多个远程服务器比如https://github.com/wangjian2014/wjtest/blob/master/wj.txt添加public密钥 本地服务器添加private密钥     S ...

  6. linux执行文件命令

    1.如果path中有你的程序所在的目录,那么直接执行filename即可 2.如果path中没有程序所在目录,那么进入目录./filename或者path/filename 比如 wj@ubuntu: ...

  7. 在ASP.NET中使用一般处理程序生成验证码

    如果期望一般处理程序(ashx)处理Session,必须实现[System.Web.SessionState]命名空间下的[IRequiresSessionState]接口. asp.net中的验证码 ...

  8. js中如何把字符串转化为对象

    例如   [javascript]  var test='{ colkey: "col", colsinfo: "NameList" }'       var ...

  9. Linux下批量替换文件内容方法

    1:查找find . -type f -name "*.html"|xargs grep ‘yourstring’ 2:查找并替换find -name '要查找的文件名' | xa ...

  10. 解决“Word无法访问您试图使用的功能所在的网络位置”问题

    解决“Word无法访问您试图使用的功能所在的网络位置”问题 打开Word时出现现现在的对话框,按取消,又可以打开word文档 按取消时,仍然可以打开word文档.为了解决这个问题,我借助网络,知道这是 ...