【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 ...
随机推荐
- Adoquery的 moveby和GotoBookmark,RecNo
GotoBookmark 是必须存在的记录,再次返回原来那个记录的位置,但是原来的那个记录必须存在,所以不适合[删除订单后回到原来的位置],因为原来的订单已经不存在了,删除了, moveby(),从当 ...
- 构建伪Update服务器工具isr-evilgrade
构建伪Update服务器工具isr-evilgrade 现在大部分软件都提供更新功能.软件一旦运行,就自动检查对应的Update服务器.如果发现新版本,就会提示用户,并进行下载和安装.而用户往往相 ...
- git alias
alias|grep git g=git ga='git add' gaa='git add --all' gapa='git add --patch' gb='git branch' gba='gi ...
- ConcurrentHashMap如何保证线程安全
以前看过HashMap的内部实现,知道HashMap是使用Node数组+链表+红黑树的数据结构来实现,如下图所示.但是HashMap是非线程安全,在多线程环境不能够使用. 不过JDK在其并发包中为我们 ...
- Java使用logback记录日志时分级别保存文件
说明:一般情况下logback可以指定类使用什么样的级别显示输出日志,并且同一类可以指定不能级别,然后对应级别进行输出日志. 第一种配置: <?xml version="1.0&quo ...
- IntelliJ IDEA出现:java: Compilation failed: internal java compiler error的问题解决
这两处地方要同时修改成一样的. 参考: http://blog.csdn.net/u011275152/article/details/45242201
- vcl.Forms等与VCL界面有关的单元不支持跨平台
vcl.Forms等与VCL界面有关的单元不支持跨平台 midaslib也不支持
- 设计模式之状态模式(State)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 第六讲_图像分割Image Segmentation
第六讲_图像分割Image Segmentation 语义分割(semantic segmentation) 常用神经网络介绍对比-FCN SegNet U-net DeconvNet 目录 +三大数 ...
- cucumber 使用资料
1.cucumber reporting github:https://github.com/damianszczepanik/cucumber-reporting 配置:详细参考上述地址描述 a.添 ...