题目:

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<A.length;i++){
 9             if(i>lastcover){
                 step++;
                 lastcover = maxcover;
             }
             
             if(A[i]+i>maxcover)
                 maxcover = A[i]+i;
         }
         
         if(maxcover<A.length-1)
             return 0;
         return step;
     }

Jump Game II leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  3. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  4. Palindrome Partitioning II Leetcode java

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  5. Remove Duplicates from Sorted List II leetcode java

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...

  6. Permutations II leetcode java

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  7. Ugly Number II leetcode java

    问题描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fa ...

  8. Word Ladder II leetcode java

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

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

随机推荐

  1. SpringBoot常用配置

    前言:springboot集成了主流的第三方框架,但是需要使用springboot那一套配置方式.但是我这里只列举了非常非常常用的,可以看已发的几篇博客,慢慢会补充.当然官方文档里也有相应的配置,可惜 ...

  2. Python 中的面向对象和异常处理

    在之前我们已经说过了 Python 中内置的主要的几种对象类型,(数,字符串,列表,元组和字典).而面向对象的核心人物还没出场呢 .那么我们常说的对象是什么类型的呢,其实他的类型就是“类”.继承封装和 ...

  3. 洛谷——P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  4. springBoot application.properties 基础配置

    # 文件编码 banner.charset= UTF-8 # 文件位置 banner.location= classpath:banner.txt # 日志配置 # 日志配置文件的位置. 例如对于Lo ...

  5. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  6. windows下安装awstats来分析apache的访问日志

    一.啰嗦两句 之前在Windows下用Apache时,也曾经配置过Awstats,然后换了工作,改用Linux+nginx,渐渐把Apache忘记了.又换了工作,又得用Apache,这回版本更新到2. ...

  7. poj 1509

    求一个字符串在旋转置换群下最小字典表示. 用的是后缀数组(后缀自动机还是再听听jason_yu讲讲吧,关于right集合的部分还有问题) 最小表示法的思想很有好(判断两个对象在某一置换群划分下,是否等 ...

  8. URAL 1881 Long problem statement

    1881. Long problem statement Time limit: 0.5 secondMemory limit: 64 MB While Fedya was writing the s ...

  9. tomcat-调整内存参数

    查看Tomcat的默认内存参数: <% /; /; /; out.println("max="+max); out.println("total="+to ...

  10. python类型比较的3种方式(转)

    通过types模块的类成员来判断,其实所有python中的类型都是这个types模块中类型的实例. import types type(x) is types.IntType # 判断是否int 类型 ...