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

这道题目跟第一道题目有区别,如果用DP动态规划来解决的话,会出现超时的问题。。也有可能是自己用的不好,后面发现一种线性遍历获得最小步骤的方法

DP 没有被AC

public class Solution {
public int jump(int[] A) {
if(A.length==0)
return 0;
if(A[0]>=A.length)
return 1;
int[] step = new int[A.length];
Boolean[] bol = new Boolean[A.length]; step[0]=0;
bol[0]=true;
for(int i=1;i<A.length;i++){
bol[i]=false;
step[i]=0;
for(int j=0;j<i;j++){
if(bol[j]&&(A[j]+j)>=i){
bol[i]=true;
if((A[j]+j)>=A.length-1)
return step[j]+1;
int temp = step[j]+1;
if(step[i]==0){
step[i]=temp;
}else{ if(step[i]>temp)
step[i]=temp;
}
}
}
}
return step[A.length-1]; }
}

线性时间AC、

public class Solution {
public int jump(int[] A) {
if(A.length==0)
return 0;
if(A.length==1){
return 0;
} int[] step = new int[A.length];
step[0]=0;
int cur =0;
Arrays.fill(step, 0);
for(int i=0;i<A.length;i++){
int temp = i+A[i];
if(temp>=A.length-1){
return step[i]+1;
}
for(int j=cur+1;j<=temp;j++){
step[j]=step[i]+1;
}
if(temp>=cur)
cur=temp;
}
return step[A.length-1];
}
}

【LeetCode】Jump Game II的更多相关文章

  1. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【LeetCode】90. Subsets II 解题报告(Python & C++)

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

  3. 【leetcode】Jump Game I & II (hard)

    Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...

  4. 【leetcode】Jump Game I, II 跳跃游戏一和二

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

  5. 【LeetCode】跳跃游戏II

    [问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...

  6. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  7. 【LeetCode】基本计算器II

    [问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  .整数除法仅保留整数部分. 输入: "3+2*2" ...

  8. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  9. 【LeetCode】52. N-Queens II(位运算)

    [题意] 输出N皇后问题的解法个数. [题解] 解法一:传统dfs回溯,模拟Q放置的位置即可,应该不难,虽然能通过,但是时间复杂度很高. 解法二:位运算大法好! 首先要明白这道题里两个核心的位运算 1 ...

随机推荐

  1. 小程序-支持的最小像素px

    经过我手机多次测试,支持的最小px为: 8px;

  2. formValidator阻止提交跳转

    formValidator这个前台校验插件非常好用,其中有几个很有特点的方法可以单独提出使用,效果非常棒这里要说的是其核心方法之一,阻止提交动作,先校验,校验成功再执行提交动作 $("#ph ...

  3. iOS开发 编码规范

    转至   http://www.cnblogs.com/celestial/archive/2012/06/30/2571417.html 编码规范 一.文档结构管理 1.建立Libraries文件夹 ...

  4. 第六讲_图像分割Image Segmentation

    第六讲_图像分割Image Segmentation 语义分割(semantic segmentation) 常用神经网络介绍对比-FCN SegNet U-net DeconvNet 目录 +三大数 ...

  5. ajax——dom对xml和html的操作

    上篇文章说到了dom的基础,dom能够操作xml和html,这次主要写利用dom的api去如何去操作xml和html文档. dom操作xml dom操作xml文档之前必须把xml文档装载到xml do ...

  6. discuz X3.1+Apache2.2+php-5.2.17+mysql5.6.14+Discuz_X3.1

    discuz X3.1+Apache2.2.25+php-5.2.17+mysql5.6.14+Discuz_X3.1 一.准备 1.httpd-2.2.25-win32-x86-no_ssl.msi ...

  7. 为Redmine的项目加上起止时间

    没有时间约束的项目不是好项目. 要给项目配置起止时间,须要用到自己定义属性. 我们须要管理员身份登录.才干够定义自己定义属性. 自己定义属性 看图吧,先是点击页面导航条(最上面那排菜单,有主页.我的工 ...

  8. 【每日Scrum】第六天(4.27) TD学生助手Sprint2站立会议

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天和楠哥做了课程事件和日历表操作的例子,并尝试做时间表和日历表的数据库设计 Y 刘静 今天开始编辑自己项目中的日历管理 编辑程序,能够在日历 ...

  9. [单元測试]_[VC2010使用gtest单元測试入门]

    场景: 1. gtest作为C++的单元測试工具非常优秀了,它集成了非常多标准assert所没有的功能,比方让流程继续运行的EXPECT,仅仅測试特定測试用例的--gtest_filter, 输出xm ...

  10. Nginx学习——进程模型(master 进程)

    进程模型 Nginx分为Single和Master两种进程模型.Single模型即为单进程方式工作,具有较差的容错能力,不适合生产之用.Master模型即为一个master进程+N个worker进程的 ...