【LeetCode】Jump Game II
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的更多相关文章
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【leetcode】Jump Game I & II (hard)
Jump Game (middle) Given an array of non-negative integers, you are initially positioned at the firs ...
- 【leetcode】Jump Game I, II 跳跃游戏一和二
题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...
- 【LeetCode】跳跃游戏II
[问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】基本计算器II
[问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 .整数除法仅保留整数部分. 输入: "3+2*2" ...
- 【LeetCode 】N皇后II
[问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...
- 【LeetCode】52. N-Queens II(位运算)
[题意] 输出N皇后问题的解法个数. [题解] 解法一:传统dfs回溯,模拟Q放置的位置即可,应该不难,虽然能通过,但是时间复杂度很高. 解法二:位运算大法好! 首先要明白这道题里两个核心的位运算 1 ...
随机推荐
- Scrapy学习-14-验证码识别
3种实现方案 1. 编码实现 tesseract-ocr 谷歌开源的识别工具,自己实现代码编码,投入精力大,回馈低.且平台验证码更换周期短,编好的代码容易失效 2. 在线打码 在线平台提供,识别率 ...
- yii执行流程
yii执行流程 原文:http://www.cnblogs.com/bluecobra/archive/2011/11/30/2269207.html 一 目录文件 |-framework 框 ...
- [专题总结]数位DP
总结: 1:第i个数符合要求了,所以接下来的数都可以.如果没限制, 那么是有 10i-1 个.如果有限制,那么是 (nowx % 10i-1)+1 . 2:两种状态设置 有设状态d ...
- Python Challenge 第三关
进入第三关,还是一张图加一句话:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. 图 ...
- POI2004
11th Polish Olympiad in Informatics(POI2004) <br > 填坑计划第二弹......把这个没填完的坑搬过来啦~ 上次勉强填完NEERC的坑... ...
- Play框架的用户验证。
最近刚刚参与一个基于Play框架的管理平台的升级工作,其中涉及到了用户的验证工作.第一次接触play框架,直接看已有代码,有点晕.因此,自己实现了一个简单的用户验证功能. 首先,新建一个User类,包 ...
- ChsLLVMDocs
https://github.com/wuye9036/ChsLLVMDocs/blob/master/CodeGen.md
- Microsoft SQL Server 2005技术内幕:存储引擎笔记
http://www.cnblogs.com/lyhabc/articles/3942053.html
- 更新tensorflow支持GPU时出错
sudo pip install --upgrade tensorflow-gpu Operation not permitted: '/tmp/pip-Sx_vMg-uninstall/System ...
- Keras 最新《面向小数据集构建图像分类模型》
本文地址:http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html ...