题目:

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. HTTP/FTP压力测试工具siege

    HTTP/FTP压力测试工具siege   压力测试可以检测服务器的承载能力.针对HTTP和FTP服务,Kali Linux提供专项工具siege.该工具可以模拟多个用户同时访问同一个网站的多个网页, ...

  2. ELK收集openstack日志

    1.安装jdk 每个openstack服务器需要安装jdk,我安装的版本jdk-7u71-linux-x64.rpm 2.安装.配置Elastic Search install https://dow ...

  3. springBoot 自动配置原理

    在之前文章中说过,springBoot会根据jar包去添加许多的自动配置,本文就来说说为什么会自动配置,自动配置的原理时什么? springBoot在运行SpringApplication对象实例化时 ...

  4. Vue+Express实现前后端分离

    先说明一下缘由,因为自己前段时间在实习,实习期间为了参与项目开发,粗略学习了下Vue.Vuex.Vue-Router,大致会一些基础的.这里也快要做毕业设计了,趁着放假回来的这两天,学习下Node的相 ...

  5. python开发_configparser_解析.ini配置文件工具_完整版_博主推荐

    # # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...

  6. 【原】MySQL实用SQL积累

    [文档简述] 本文档用来记录一些常用的SQL语句,以达到快速查询的目的. [常用SQL] 1.mysql数据库中获取某个表的所有字段名 select COLUMN_NAME from informat ...

  7. MySQL的mysql.sock文件作用(转)

    mysql.sock是可用于本地服务器的套接字文件.它只是另一种连接机制. 不包含任何数据,但仅用于从客户端到本地服务器来进行交换数据.

  8. HowTo: Restart SSH Service under Linux / UNIX

    How do I restart SSH service under Linux or UNIX operating systems? The command to restart ssh are a ...

  9. Turtelizer 2 provide JTAG Flash programming and debugging of ARM based boards via USB

    http://www.ethernut.de/en/hardware/turtelizer/ Introducing Turtelizer 2 Overview Turtelizer 2 had be ...

  10. java 盒子模型

    http://www.cnblogs.com/xiaohuochai/tag/javascript%E6%80%BB%E7%BB%93/