Leetcode::JumpGame
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的更多相关文章
- leetcode — jump-game
/** * Source : https://oj.leetcode.com/problems/jump-game/ * * Created by lverpeng on 2017/7/17. * * ...
- LeetCode: JumpGame 1 and 2
Title : Given an array of non-negative integers, you are initially positioned at the first index of ...
- [Leetcode 55]跳格子JumpGame
[题目] Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [leetcode]55.JumpGame动态规划题目:跳数游戏
/** * Given an array of non-negative integers, you are initially positioned at the first index of th ...
- [LeetCode] Jump Game 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- [LeetCode]题解(python):055-Jump Game
题目来源 https://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initi ...
随机推荐
- Effective C++ 7
7.预先准备足够的内存情况. new当内存分配请求无法完成,它会抛出一个异常,怎么办异常,这是一个非常现实的,绝对必要的所遇到的问题后. 于c一般使用宏来分配内存和测试分发成功.c++中产阶级似下面的 ...
- zTree市县实现三个梯级DAO接口测试
zTree市县实现三个梯级DAO接口测试 ProvinceDaoTest.java: /** * @Title:ProvinceDaoTest.java * @Package:com.gwtjs.da ...
- React.js终探(五)
在React中,一切都是看做组件. 而组件的嵌套也是十分常见的. 所以有的组件就作为容器组件 容器组件 React元素可以包含子元素 如 //JSX <ezpanel title="t ...
- WebApi路由及版本控制
public class WebApiControllerSelector : IHttpControllerSelector { private const string NamespaceKey ...
- 【Android接口实现】PhotoView——单点支持/多图像缩放,实现了触摸
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 今天给大家介绍的开源项目,是来自Github的PhotoView项目,这个项目的主要功能是实现普通的Imag ...
- 搭建一个BS 的简单SOA 架构(直接通过jquery 调用后台的 wcf 服务的架构)(第一天)
亲们!还在用传统的三层架构吗?你还在对SOA架构 不了解吗? 那就赶快来学习下一个 比较简单的SOA的架构吧!我会手把手的 教会你们怎么搭建这个 简单的SOA的架构. 其中用的技术点保证 WCF,a ...
- app后端设计(0)--总文件夹
原文:http://blog.csdn.net/newjueqi/article/details/19003775 做了接近两年app相关的系统架构,api设计,先后在两个创业公司中工作,经历过手机网 ...
- WebBrowser控件应用:播放PPT文件
原文:WebBrowser控件应用:播放PPT文件 一开始想的是用webform来做,用iframe加载文件,把ppt文件另存成htm,然后播放. 可是后来发现,的程序不大容易控制,所以改用winfo ...
- 基于jsoup的Java服务端http(s)代理程序-代理服务器Demo
亲爱的开发者朋友们,知道百度网址翻译么?他们为何能够翻译源网页呢,iframe可是不能跨域操作的哦,那么可以用代理实现.直接上代码: 本Demo基于MVC写的,灰常简单,copy过去,简单改改就可以用 ...
- inux平台的C与C++
课堂里学不到的C与C++那些事(一) 首先,声明一下这是一个系列的文章.至于整个系列有多少篇,笔者也不知道,不知道有多少篇,也不知道多久会更新一篇.反正只有一个原则,写出来的文 章能见得人才会公布出来 ...