Description:

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.

分析: 此题上来直接的解法,是建一个bool的vector,然后从每一点出发,按照该点的值来更新后面的vector里面的bool值

然后看最后一点是否为false.

这样当然是可以的,但是这样每一点都要往后遍历此点值步,最坏情况时 n+n-1 + n-2 +... +1,即O(n^2),最终会超时。

所以我们要建立的遍历一遍就能得到结果的方法。其方法很简单:维护一个maxstep变量,即当前点及之前所有点能够前进的最大值,

这个值每步更新为 maxstep = max(maxstep-1,A[i]); 这个值在前进道最后的点之前变为0,则结果为false.

贴上两种算法:

 //Faster version
class Solution {
public:
bool canJump(int A[], int n) { int maxrec = ; for(int i=;i<n;i++)
{
maxrec--;
if(i==n-) return true;
maxrec = max(maxrec,A[i]);
if(maxrec==)
break;
}
return false; }
};
 //TLE version
class Solution {
public:
bool canJump(int A[], int n) {
vector<bool> rec(n,false);
rec[] = true;
for(int i=;i<n;i++)
{
if(!rec[i]) continue; int step = A[i];
for(int j=;j<=step;j++)
{
rec[min(i+j,n-)]=true;
if(rec[n-])
return true;
}
} return rec[n-]; }
};

Leetcode::JumpGame的更多相关文章

  1. leetcode — jump-game

    /** * Source : https://oj.leetcode.com/problems/jump-game/ * * Created by lverpeng on 2017/7/17. * * ...

  2. LeetCode: JumpGame 1 and 2

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

  3. [Leetcode 55]跳格子JumpGame

    [题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. [leetcode]55.JumpGame动态规划题目:跳数游戏

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

  5. [LeetCode] Jump Game 跳跃游戏

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

  6. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  7. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  8. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  9. [LeetCode]题解(python):055-Jump Game

    题目来源 https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initi ...

随机推荐

  1. Cocos2d-X采用CCScrollView创建滚动视图

    CCScrollView滚动视图可以让游戏有效果,并能够通过滚动视图切换游戏场景,滚动视图通常用来选择在游戏中的级别 实例1:使用CCScrollView创建一个简单的滚动视图 首先创建一个Scrol ...

  2. hightmaps 按地图上显示的统计数据

    离extjs 至 easyui 到html5到hightchars 再到hightmaps.Exjts和easyui很相似,extjs是重量级的,easyui轻量级的.比extjs容易上手.照着dem ...

  3. MVC6项目

    解读ASP.NET 5 & MVC6系列(2):初识项目 2015-05-14 09:08 by 汤姆大叔, 2866 阅读, 19 评论, 收藏, 编辑 初识项目 打开VS2015,创建We ...

  4. 【C语言的日常实践(十六)】字符串输出功能puts、fputs和printf

    C有三个标准库函数的输出字符串puts().fputs()和printf(). 1.puts()函数仅仅须要给出字符串參数的地址. #include <stdio.h> int puts( ...

  5. CSharp设计模式读书笔记(13):代理模式(学习难度:★★★☆☆,使用频率:★★★★☆)

    代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问. 模式角色与结构: 示例代码: using System; using System.Collections.Generi ...

  6. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  7. CQRS(命令查询职责分离)和 EDA(事件驱动架构)

    转载CQRS(命令查询职责分离)和 EDA(事件驱动架构) 上一篇:<IDDD 实现领域驱动设计-SOA.REST 和六边形架构> 阅读目录: CQRS-命令查询职责分离 EDA-事件驱动 ...

  8. HDU 5045 Contest

    pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...

  9. Windows 7的 磁盘管理中,某个磁盘或分区,突然变成只读。

    1.今天突然发现E盘无法创建文件夹.文件,也不可以改,感觉像是变成只读 . 2.我的电脑 -> 计算机管理 -> 存储 -> 磁盘管理,发现E盘下面标记着只读两个字. 3.由于我的E ...

  10. 谈谈那些年PHP中屌屌的验证码

    验证码已经是现在网站中非常基础的知识点了,验证码的存在可以防止恶意破解密码.刷票.灌水,可以有效的防止暴力破解特定用户. 现在就来了解了解那些年PHP中屌屌的验证码吧. 首先,以四位验证码为例(多位验 ...