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.

最开始想像jump game I一样用动态规划,发现会超时。

所以想到了用贪心算法。

可以计算每一步可以跳到的最远距离。第一次到达数组末尾时就是最少的steps。时间复杂度为O(n).

 1 public static int jump2 (int[] A) {
 2         if (A.length == 0) return -1;
 3         int steps = 0;
 4         int index = 0;
 5         int last = -1;
 6
 7         while (index < A.length-1) {
 8             int l = index;
 9             for (int i = index; i > last; i--) {
10                 if (i + A[i] > l) l = i + A[i];
11             }
12             if (index == l) return -1;
13             last = index;
14             index = l;
15             steps++;
16         }
17
18         return steps;
19     }

也可以计算,在每个点的时候能跳到的最远的距离。第一次到达末尾时,是最少的steps。

last记录的是上一次能跳到的最远的距离。只有当i超过last的时候,说明已经超过了上一次跳跃的最大距离,是又发生了一次跳跃。所以steps++。

 1 public int jump(int[] A) {
 2         int steps = 0;
 3         int index = 0;
 4         int last = -1;
 5
 6         for (int i = 0; i < A.length; i++) {
 7
 8             if (index >= A.length - 1) {
 9                 break;
10             }
11             if (i > last) {
12                 steps++;
13                 last = index;
14             }
15             if (i + A[i] > index) {
16
17                 index = i + A[i];
18             }
19         }
20
21         return steps;
22     }

转载于:https://www.cnblogs.com/longhorn/p/3603013.html

LeetCode: 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 ...

  2. 【To Read】LeetCode | Jump Game II(转载)

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

  3. 【暴力枚举】LeetCode 90. Subsets II

    LeetCode 90. Subsets II solution1和2均是利用set的,3和4是不利用set的 Solution1:我的答案 迭代法 class Solution { public:v ...

  4. 【DFS】LeetCode 52. N-Queens II

    LeetCode 52. N-Queens II Solution1:我的答案 教科书一样的回溯法 <程序员面试金典>中有一道一毛一样的题啊! class Solution { publi ...

  5. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

  6. Jump Game/Jump Game II

    题目分析 1. Jump Game I和 Jump Game II放在一起做. 2. 一开始还考虑是不是要用动态规划来解答,因为看上去非常像是要动态规划的,但是后来考虑觉得用贪心的思路来做就可以了.我 ...

  7. Jump Game Jump Game II

    Jump Game 解法:贪心+状态记录 class Solution {public boolean canJump(int[] nums) {int index=0;for(int i=0;i&l ...

  8. LeetCode Jump Game Jump GameII

    LeetCode Jump Game && Jump GameII 又是每日LeetCode Time, 这里直接解决Jump Game和Jump GameII; 先上题目: Jump ...

  9. [LeetCode] Word Break II 拆分词句之二

    [LeetCode] Word Break II 拆分词句之二 Given a string s and a dictionary of words dict, add spaces in s to ...

最新文章

  1. 报错:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This versio
  2. simplified build configuration
  3. 新手程序员不知道的小技巧!
  4. conn.setAutoCommit(true) and conn.close() 关系
  5. 【BZOJ 1503】郁闷的出纳员【权值线段树】
  6. java数组base64编码,java将base64编码字符串还原为字节数组
  7. 麻省理工成立金融科技实验室,蚂蚁金服成唯一中国创始企业
  8. 人工智能搜索算法案例分析
  9. [Swift]Swift中的extension
  10. java取当前北京时间_用Java取指定时区的时间 北京时间,纽约时间,班加罗尔时间...
  11. ipp协议 服务器,IPP远程服务
  12. SIGCHLD信号(重点)
  13. openFoam+paraview 显示网格cellID
  14. 病毒木马查杀实战第010篇:QQ盗号木马之十六进制代码分析
  15. 两个质数互质是_两个质数一定是互质数_互质数和质数的区别_分解质因数的方法_互为质数和互质数...
  16. 2021阳城一中高考成绩查询,2019阳城一中录取分数线(附2019高考成绩喜报)
  17. Python语法对空格的严格要求
  18. sklearn_feature_selection
  19. [电商实时数仓] 数据仓库建模过程分析
  20. 格林威治时间(GMT) 字符串转Date

热门文章

  1. C++析构函数不能失败的4个理由
  2. DV录像带导出一定要用1394
  3. EntityFramework中实体类到表名的批量映射
  4. 【转】JS回调函数--简单易懂有实例
  5. [转帖]Docker里运行Docker docker in docker(dind)
  6. js事件循环 microtask macrotask
  7. 大道至简:软件工程实践者的思想——第七、八章感想
  8. JSP-03-实现数据传递
  9. 在asp.net2.0中使用串行化对象实现自定义配置
  10. RollingFileAppender