【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 ...
随机推荐
- P1136 超车 归并排序 求逆序对个数
这道题从看到它开始到做出来,已经过了快两周[因为第一次思路完全跑偏写的是暴力模拟想水过]: 题意是这样的: jzabc除了对多米诺骨牌感兴趣外,对赛车也很感兴趣.上个周末他观看了一场赛车比赛.他总是 ...
- 2017 [六省联考] T2 相逢是问候
4869: [Shoi2017]相逢是问候 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 1205 Solved: 409[Submit][Stat ...
- Endless Pallet(min-max容斥)
地址:传送门 分析: 设$x_i$表示第i个点被染成黑色的时间,所求即为$E(max \left \{x_i \right \})$ 因为$E(X)=\sum_{k=1}^{\infty}i \ti ...
- 记一次痛苦的ubuntu配置Go环境 -- Ubuntu & Go
下载Go一定到: https://golang.org/dl/ 这里, 其他的都不好使. 还有go不一定跟32位和64有关, 还和CPU架构有关, 不知道架构一个一个试, 亦可以看我的关于查看Ubu ...
- 【经验之谈】适合学习的IT教程站点列表
① CSDN http://www.csdn.net/ ② gitHub https://github.com/ ③ 极客学院 http://www.jikexueyuan.com/ ...
- SilverLight:基础控件使用(2)-ComboBox,ListBox控件
ylbtech-SilverLight-Basic-Control:基础控件使用(2)-ComboBox,ListBox控件 直接在 XAML 代码中设置 Items 和通过后台代码绑定数据源 Com ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- 怎样隐藏Windows7 系统保留分区
安装Windows7操作系统时须要预留出100MB左右的系统保留盘分区.在Windows7激活是必须给它分配盘符,否则无法将其成功激活,但是激活后该盘符永久地显示了出来,怎样将其隐藏掉呢? 1.隐藏前 ...
- S3C2440 IIS操作 uda134x录放音
IIS(Inter-IC Sound)由飞利浦公司开发.是一种经常使用的音频设备接口,主要用于CD.MD.MP3等设备. s3c2440一共同拥有5个引脚用于IIS:IISDO.IISDI.IISSC ...
- nyoj43 24 Point game(DFS)
题目43 题目信息 pid=43" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 讨论 ...