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.

贴上两种算法:


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

Leetcode::JumpGame

时间: 12-06

Leetcode::JumpGame的相关文章

LeetCode: JumpGame 1 and 2

Title : 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 exam

[LeetCode]55.Jump Game

题目 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:

Leetcode分类总结(Greedy)

贪心类题目目前除了正则匹配(Wildcard Matching)(据说其实是DP)那道还没做其他的免费题目都做了,简单做个总结. 贪心的奥义就是每一步都选择当前回合”可见范围“(即可得知的信息)内的最优,而在每一步都仅选择当前回合”可见范围“内的最优这一策略下能够导致全局最优的结果的情况使用贪心就会是正确的,否则不适用贪心(或不适用当前对贪心中的最优的定义). 因此,贪心一个点是选择当前最优,另一个点是这个最优要怎么定义,比如是选使得A最小的还是选使得A-B或A/B最小的等等. 贪心的正确性其实

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

[Leetcode][Python]55: Jump Game

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 55: Jump Gamehttps://leetcode.com/problems/jump-game/ Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents

[leetcode]Jump Game @ Python

原题地址:https://oj.leetcode.com/problems/jump-game/ 题意: 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 y

[LeetCode][JavaScript]Jump Game

https://leetcode.com/problems/jump-game/ Jump Game 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

[LeetCode] 动态规划入门题目

最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法",其实我认为把它称作一种"思想"更为合适:利用动态规划去解决问题,其实就是逐步递推的过程,与贪心算法不同,动态规划递推的每一步都要求是当前的最优解(这是很重要的,递推的正确性依赖的就是这一点):利用动态规划解题时,必须自己定义出来状态和状态转移方程.然而,看上去简单,做起来却非常困难,因为

LeetCode 55/45 Jump Game I/II-----Greedy**

一:Jump Game 题目: 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.