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. 第20章 状态模式(State Pattern)

    原文 第20章 状态模式(State Pattern) 状态模式  概述:   当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态的条件表 ...

  2. Jquery实现 TextArea 文本框根据输入内容自动适应高度

    原文 Jquery实现 TextArea 文本框根据输入内容自动适应高度 在玩微博的时候我们可能会注意到一个细节就是不管是新浪微博还是腾讯微博在转发和评论的时候给你的默认文本框的高度都不会很高,这可能 ...

  3. nodejs爬虫系统

    其中express是服务端框架 request相当于前端的ajax请求 cheerio相当于jq 开始 首先我们先新建一个 crawler目录 执行 npm install express -g 命令 ...

  4. ReactJs入门思路

    ReactJs入门思路小指南 原文  http://segmentfault.com/blog/fakefish/1190000002449277 React是怎么搞的? React中,把一切东西都看 ...

  5. java_Eclipse主题颜色配置+全屏

    http://www.eclipsecolorthemes.org/ 这个是主题的网站. 在Eclipse里, File->Import->General->Preferences- ...

  6. 用python+selenium导入excel文件

    连接mysql #encoding=utf-8 import pymysql import time class ConnMysql(object): def __init__(self): self ...

  7. Samza/KafkaAnalysizing

    Apache Samza is a distributed stream processing framework. It uses Apache Kafka for messaging, and A ...

  8. 算法 & 分析 (收集)

    算法一:快速排序算法 快速排序是由东尼·霍尔所发展的一种排序算法.在平均状况下,排序 n 个项目要Ο(n log n)次比较.在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见.事实上,快速排序通 ...

  9. 非常多人不愿意承认汉澳sinox已经超过windows

    汉澳sinox採用的zfs和jail打造高可靠性存储server和矩阵计算机,这不是windows和linux能相提并论的. 只是非常多人立即出来出来反驳说,windows驱动程序多(就是支持硬件多) ...

  10. 使用 C# 进行 Outlook 2007 编程

    原文:使用 C# 进行 Outlook 2007 编程 探讨如何使用 C# 编程语言生成 Outlook 识别的应用程序和 Outlook 外接程序. 请从"Add References&q ...