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. Android4.4 Framework分析——startService创建过程

    我们经常使用context.startService()要启动service.下面就来分析这service启动过程,下图是service启动序列图: watermark/2/text/aHR0cDov ...

  2. Swift中文手册 -- The Basics

    原文:Swift中文手册 -- The Basics 基础部分 Swift 是 iOS 和 OS X 应用开发的一门新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 S ...

  3. 开发者:网站 & SDK

    { 收集的一些.开发工具 } Teambition 团队协作工具 GitCafe 代码托管 FIR.im App 托管平台 Coding 代码托管,项目管理,WebIDE 计蒜客 编程学习 SendC ...

  4. Android_开发人员经常使用的颜色

    Android开发中经常要用一些个性化的颜色,然而茫茫的RBG颜色对比表,往往给人眼花缭乱的感觉,更别说从中轻易选出一两种比較惬意的颜色,以下我就总结一下开发中经常使用到的比較绚丽的颜色. 以下是经常 ...

  5. POJ 3047 Bovine Birthday 日期定周求 泽勒公式

    标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...

  6. NSNotification、delegate和KVO的区别

    1.效率:delegate比nsnotification高.2. delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值, 也就是delegat ...

  7. 菜鸟进阶Android Touch事件传递(四)

    尊重他人劳动成果,转载请说明出处:http://blog.csdn.net/bingospunky/article/details/44343477 在该系列文章第四篇.我准备介绍一下viewpage ...

  8. 新秀nginx源代码分析数据结构篇(两) 双链表ngx_queue_t

    nginx源代码分析数据结构篇(两) 双链表ngx_queue_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...

  9. leetcode -day31 Subsets I II

    1.  Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a ...

  10. C#在outlook里创建一封邮件到草稿箱

    原文:C#在outlook里创建一封邮件到草稿箱 1.引用Microsoft.Office.Interop.Outlook.dll 2.  实现代码 public static int SendToD ...