Jump Game

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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

 

算法1:暴力解法,注意A[0] = 0的边界条件.该解法O(n^2),大数据超时了

class Solution {
public:
bool canJump(int A[], int n) {
if(n == 1)return true;
else if(A[0] == 0)return false;
bool canArrive[n];
memset(canArrive, 0, sizeof(canArrive));
canArrive[0] = true;
for(int i = 0; i < n; i++)
{
if(canArrive[i] == false)continue;
int farest = min(i + A[i], n - 1);
for(int j = i + 1; j <= farest; j++)
canArrive[j] = true;
if(canArrive[n-1])return true;
}
return canArrive[n-1];
}
};

 

算法2:优化解法,只需要顺序扫描数组,记录下能够到达的最远位置

class Solution {
public:
bool canJump(int A[], int n) {
int canArrive = 0;//当前能到达的最远位置
for(int i = 0; i <= canArrive && canArrive < n-1; i++)
if(i + A[i] > canArrive)canArrive = i + A[i];
return canArrive >= n-1;
}
};

 


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

 

算法3:在上述算法1的基础上(其实是动态规划,minjumps[i] = min{minjumps[k] + 1},k<i 且 i+A[k]>=i )                            本文地址

class Solution {
public:
int jump(int A[], int n) {
vector<int> minjumps(n, INT_MAX);
minjumps[0] = 0;
for(int i = 0; i < n; i++)
{
int farest = min(i + A[i], n - 1);
for(int j = i + 1; j <= farest; j++)
if(minjumps[j] > minjumps[i] + 1)
minjumps[j] = minjumps[i] + 1;
}
return minjumps[n-1];
}
};

 

算法4:在上述算法2的基础上(具体解释可参考http://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html

class Solution {
public:
int jump(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int canArrive = 0, res = 0, lastCanArrive = 0;
for(int i = 0; i < n; i++)
{
if(i > lastCanArrive)
{
res++;
lastCanArrive = canArrive;
}
if(i + A[i] > canArrive)
canArrive = i + A[i];
}
return res;
}
};

 

稍微改进一下,只要canArrive >= n-1 ,就可以结束循环,此时返回值是res+1

class Solution {
public:
int jump(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n == 1)return 0;
int canArrive = 0, res = 0, lastCanArrive = 0;
for(int i = 0; canArrive < n-1; i++)
if(i + A[i] > canArrive)
{
if(i > lastCanArrive)
{
res++;
lastCanArrive = canArrive;
}
canArrive = i + A[i];
}
return res+1;
}
};

 

算法5:从最后一个开始,找到第一个能到最后的,再往前找第一个能到新的位置的,直到第0位(参考http://www.laurashawn.net/?p=10885

class Solution {
public:
int jump(int A[], int n) {
int i=n-1;
int step=0;
while(i>0){
for(int j=0;j<i;j++){
if(A[j]+j>=i){
step++;
i=j;
break;
}
}
}
return step;
}
};

 

 

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3719630.html

LeetCode:Jump Game I II的更多相关文章

  1. leetcode Jump Game I II 待续 贪心看不懂啊!!!!

    下面是这两个题的解法: 参考博客:http://blog.csdn.net/loverooney/article/details/38455475 自己写的第一题(TLE): #include< ...

  2. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  3. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  4. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  5. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  6. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  7. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  8. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

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

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

随机推荐

  1. Java静态同步方法和非静态同步方法

             所有的非静态同步方法用的都是同一把锁——该实例对象本身.也就是说如果一个实例对象的非静态同步方法获取锁后,该实例对象的其他非静态同步方法必须等待获取锁的方法释放锁后才能获取锁进而执行 ...

  2. java Hello 出现以下结果:Bad command or the file name 可能是什么原因

    没有这个命令或文件名 原因可能是没有成功安装jdk或者没有配置好jdk 的环境变量,或者没有编译相应的文件. 2. 出现以下结果:Exception in thread “main” java.lan ...

  3. subline 快捷键

    subline 快捷键  安装 pretty css  html  后1,CTRl+ shift +H 格式化代码

  4. Github学习进阶-初露锋芒,通过命令行将本地git仓库推送到Github上面的仓库

    前提: 1. 需要安装git 客户端.  能打开 git  bash 命令行窗口. 2. 生成了ssh 秘钥,并添加到了Github上面. 一.在Github上面建立一个git仓库. 点击 + 号,在 ...

  5. Redis系列(三)—— 订阅/发布

    Redis 订阅/发布 参考:http://www.cnblogs.com/mushroom/p/4470006.html,http://www.tuicool.com/articles/ABry2a ...

  6. 如何用ZBrush雕刻出栩栩如生的头发(一)

    之前的ZBrush教程我们学习了使用SubTool为模型添加了头发效果,本讲对模型头发雕刻技巧和细节进行调整.文章内容仅以fisker老师讲述为例,您也可以按照自己的想法,跟着老师的步调进行创作,发挥 ...

  7. 如何在ZBrush中添加毛发

    ZBrush不仅能雕刻出完美的头发造型,还能够应用真实的头发和毛发.在制作毛发之前只需要简单定义遮罩区域,包括长短.疏密.当然,最重要的是,你可以使用Polypaint生成各种有色纤维,这将非常方便. ...

  8. 读《深入理解Java虚拟机》有感——第一部分:Class文件的结构

    1.产生 源码(.java文件)——>编译器(如:javac)——>字节码(.class文件)——>虚拟机(如:HotSpot)执行 2.Class文件  1)构成:         ...

  9. codeforces 713A A. Sonya and Queries(状态压缩)

    题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...

  10. 原创翻译-测试驱动开发(TDD)

    测试驱动开发原则 翻译自<<Expert Python Programming>> 测试驱动开发是指首先编写包含所有测试软件特点的测试集,然后再去开发软件.也就是说,在编写软件 ...