题目:

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

题解:

参考了http://blog.csdn.net/linhuanmars/article/details/21356187,这道题和Jump Game都是利用动态规划的思想。区别是,上一道题维护的全局最优是maxcover,一旦maxcover大于总长度,那么说明能跳到结尾。

而这道题除了维护maxcover外,还需要考虑维护最小步数,最小步数的维护靠maxcover作为每一步能跳的长度,代码如下:

1 public int jump(int[] A) {

2         if(A==null||A.length==0)

3             return 0;

4

5         int maxcover = 0;

6         int step = 0;

7         int lastcover = 0;

8         for(int i = 0; i<=maxcover&&i

9             if(i>lastcover){

10                 step++;

11                 lastcover = maxcover;

12             }

13

14             if(A[i]+i>maxcover)

15                 maxcover = A[i]+i;

16         }

17

18         if(maxcover

19             return 0;

20         return step;

21     }

jump game java_Jump Game II leetcode java相关推荐

  1. Path Sum II leetcode java

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  2. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  3. Binary Tree Level Order Traversal II leetcode java

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  4. Reverse Linked List II leetcode java

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1 ...

  5. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  6. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

  7. LeetCode算法题-Valid Palindrome II(Java实现)

    这是悦乐书的第287次更新,第304篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第155题(顺位题号是680).给定非空字符串s,最多可以删除一个字符. 判断它是否是回 ...

  8. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  9. leetcode 240. Search a 2D Matrix II | 240. 搜索二维矩阵 II(Java)

    题目 https://leetcode.com/problems/search-a-2d-matrix-ii/ 题解 方法1 思路类似于 leetcode 200. Number of Islands ...

最新文章

  1. java map to map 拷贝_java复制Map 对象(深拷贝和浅拷贝)
  2. @Service注解的使用
  3. 【视频】如何正确焊接贴片、直插元件?
  4. linux编译准备,Linux 下编译指南
  5. linux硬盘转windows7,记——第一次上手UEFI电脑,将mbr硬盘的Windows7和Linux转为gpt+uefi启动...
  6. Unity4.6证书激活问题
  7. mysql主从复制及问题解决
  8. 比较Apache Hadoop 生态系统中不同的文件格式和存储引擎的性能
  9. 图书管理系统C++程序源代码
  10. java adminlte 使用_AdminLTE框架基础布局使用
  11. 最新最全张家界旅游攻略(您必须知道的张家界攻略)
  12. integrate函数python_scipy.integrate Pseudo-Voigt函数,积分变为0
  13. python实例属性没有init_处理init外部定义的实例属性的更好/更python方式?
  14. 124-移动端游乐园项目
  15. ECC-椭圆曲线密码
  16. linux7.4修改密码,Centos7.4 修改Root密码
  17. 电子硬件工程师要求?
  18. 数据库常用的命令讲解
  19. 互联网人:我太太太太太太太太太太难了!
  20. java conversion_java.util.UnknownFormatConversionException: Conversion = ''';

热门文章

  1. python精进之路 -- open函数
  2. 度量、符号、转换关系
  3. [SOJ] 简单哈希
  4. 初步理解TCP/IP网络
  5. 邮件被暴力破解邮件网关如何解决
  6. 获取AppStore上架后的应用版本号
  7. golang if 条件判断语句 简介
  8. linux top 报错 TERM environment variable not set.
  9. linux 磁盘维护 swapon swapoff 简介
  10. 区块链 重放攻击 简介