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.

贴上两种算法:

  1. //Faster version
  2. class Solution {
  3. public:
  4. bool canJump(int A[], int n) {
  5.  
  6. int maxrec = ;
  7.  
  8. for(int i=;i<n;i++)
  9. {
  10. maxrec--;
  11. if(i==n-) return true;
  12. maxrec = max(maxrec,A[i]);
  13. if(maxrec==)
  14. break;
  15. }
  16. return false;
  17.  
  18. }
  19. };
  1. //TLE version
  2. class Solution {
  3. public:
  4. bool canJump(int A[], int n) {
  5. vector<bool> rec(n,false);
  6. rec[] = true;
  7. for(int i=;i<n;i++)
  8. {
  9. if(!rec[i]) continue;
  10.  
  11. int step = A[i];
  12. for(int j=;j<=step;j++)
  13. {
  14. rec[min(i+j,n-)]=true;
  15. if(rec[n-])
  16. return true;
  17. }
  18. }
  19.  
  20. return rec[n-];
  21.  
  22. }
  23. };

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. React.js终探(五)

    在React中,一切都是看做组件. 而组件的嵌套也是十分常见的. 所以有的组件就作为容器组件 容器组件 React元素可以包含子元素 如 //JSX <ezpanel title="t ...

  2. AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔)

    AspNet.WebAPI.OData.ODataPQ 这是针对 Asp.net WebAPI OData 协议下,查询分页.或者是说 本人在使用Asp.Net webAPI 做服务接口时写的一个分页 ...

  3. Java设计模式偷跑系列(十二)组合模式建模和实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39828653 组合模式(Composite):组合模式有时又叫部分-总体模式.将对象组合成 ...

  4. BZOJ 3209 花神的数论题 数位DP+数论

    题目大意:令Sum(i)为i在二进制下1的个数 求∏(1<=i<=n)Sum(i) 一道非常easy的数位DP 首先我们打表打出组合数 然后利用数位DP统计出二进制下1的个数为x的数的数量 ...

  5. nginx跳转

    语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配   ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径就可以.nginx不正确url做 ...

  6. hdu 4932 Miaomiao&#39;s Geometry(暴力)

    题目链接:hdu 4932 Miaomiao's Geometry 题目大意:在x坐标上又若干个点,如今要用若干条相等长度的线段覆盖这些点,若一个点被一条线段覆盖,则必须在这条线的左端点或者是右端点, ...

  7. Nagios监控lvs服务

    1在lvs server上安装nrpe客户端: 1.1,rpm方式安装nrpe客户端 下载地址:http://download.csdn.net/detail/mchdba/7493875 [root ...

  8. 从久负盛名的GoDaddy开发革命来看Node.js的风靡程度

    英文原文连接:http://venturebeat.com/2015/02/09/godaddy-nodejitsu/ 网站主机托管公司GoDaddy将要进一步通过新的开发工具来提升自身能力.最新消息 ...

  9. python基础课程_学习笔记20:标准库:有些收藏夹——os

    标准库:有些收藏夹 os os模块为您提供访问多个操作系统服务特征. os和它的子模块os.path还包含一些用于检查.构造.删除文件夹和文件的函数,以及一些处理路径的函数. os模块中一些重要函数和 ...

  10. 老调重弹--面向对象设计原则--GRASP设计原则

    GRASP概述 GRASP,全称General Responsibility Assignment Software Patterns,译为”通用职责分配软件原则“,包含以下原则和模式 控制器(Con ...