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: A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

思路:若能走的最远距离小于当前距离,说明走不过来。否则,利用此位置情况更新最远距离。

class Solution {
public:
bool canJump(int A[], int n) {
int reached = 0;
for(int i = 0; i < n; ++i) {
if(reached < i) return false;
if(i+A[i] > reached) reached = i+A[i];
}
return true;
}
};

Jump Game II

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.

Your goal is to reach the last index in the minimum number of jumps.

For example: Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

思路:

方法1: 动态规划。(能走到下标为 n-1 位置时,就结束。)

class Solution {
public:
int jump(int A[], int n) {
if(n == 0) return 0;
vector<int> step(n, INT_MAX);
step[0] = 0;
int reached = 0;
for(int i = 0; i < n-1; ++i) {
if(reached < i || step[i] >= step[n-1]) break;
if(i + A[i] > reached) {
reached = i+A[i];
for(int j = i+1; j < n && j <= reached; ++j)
step[j] = min(step[j], step[i]+1);
}
}
return step[n-1];
}
};

方法二 : 从前往后跳,每一步长内选择选择能跳到下一步长最远的点。 第一个步长为 0 - A[0], 第二步长为 A[0] - max(0+A[0],..., A[0] + A[A[0]]),

从 0->A[0]->maxA[i](0+A[0],...,A[i] + A[A[i]], ... , A[0] + A[A[0]]);

class Solution {
public:
int jump(int A[], int n) {
int last = 0, step = 0, maxV = 0;
for(int i = 0; i < n; ++i) {
if(i > last) {
last = maxV;
++step;
}
maxV = max(maxV, i + A[i]);
}
return step;
}
};

方法三: 从后往前确定应该走的位置。(从前往后遍历,如果 i + A[i] >= last , 说明其第一个能走到最后, 更新 last = i).

class Solution {
public:
int jump(int A[], int n) {
int last = n-1, step = 0;
while(last > 0) {
for(int i = 0; i < last; ++i) {
if(i+A[i] >= last) {
last = i;
step++;
break;
}
else if(i == last) return INT_MAX;
}
}
return step;
}
};

附加:以下这个代码也可以 AC. (但是常识上不觉得是对的)

class Solution {
public:
int jump(int A[], int n) {
int last = n-1, step = 0;
while(last > 0) {
for(int i = 0; i < last; ++i) {
if(i+A[i] >= last) {
last = i;
step++;
}
}
}
return step;
}
};

57. Jump Game && Jump Game II的更多相关文章

  1. [LeetCode#55, 45]Jump Game, Jump Game II

    The problem: Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...

  3. 杭电oj 1087——super jump!jump!jump(java实现)

    question:Super Jumping! Jumping! Jumping! 意思就是找一串数字中的和最大子串 思路:创建另一个数组,每一项是路径数组对应项之前最大子串的和,然后遍历此数组找出最 ...

  4. Solution -「CodeChef JUMP」Jump Mission

    \(\mathcal{Description}\)   Link.   有 \(n\) 个编号 \(1\sim n\) 的格子排成一排,并有三个权值序列 \(\{a_n\},\{h_n\},\{p_n ...

  5. Jump Game II 解答

    Question Given an array of non-negative integers, you are initially positioned at the first index of ...

  6. 2018今日头条杯 E-Jump a Jump

    Problem E. Jump A JumpInput file: standard inputOutput file: standard outputTime limit: 1 secondsMemor ...

  7. LeedCde 题解目录

    1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...

  8. 《CODE》读后笔记——第14~20章

    14.反馈与触发器 振荡器不需要人的干涉即可自主且不断地实现断开和闭合.所有计算机都靠某种振荡器来使其他部件同步工作. 当两个开关都断开时,电路有两个稳定状态,这样的一个电路称为触发器.触发器具有记忆 ...

  9. TopCoder SRM 633div1

    250pts   PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...

随机推荐

  1. 1238. Folding

    http://acm.timus.ru/problem.aspx?space=1&num=1238 DP+记忆化搜索 思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最 ...

  2. 将HTML文本框设为不可编辑文本框。

    将HTML文本框设为不可编辑文本框. 方法1: onfocus=this.blur() <input type="text" name="input1" ...

  3. ubuntu科学计算包blas和lapack的安装

    该包是高性能的线性代数计算库,两个包一般是相互依赖,因此选择同时介绍其安装: 官方发布如今是lacpack-3.5.0.tgz,获取方法是网址.但打不开,ubuntu一般用 wget http://w ...

  4. JavaScript数组方法总结

    由于最近都在freecodecamp上刷代码,运用了很多JavaScript数组的方法,因此做了一份关于JavaScript教程的整理,具体内容如下: 一.普通方法 1.join() 将数组元素连接在 ...

  5. Unity3d_学习笔记_入门

    转自:http://blog.csdn.net/zlfxy/article/details/8722437 本文内容来自“编程教父”的视频课程. 1.Unity3d一个游戏引擎,可以用来开发很多游戏. ...

  6. cocos2dx 3.0 之 lua 创建类

    利用3.0里面封装好的模板类来写,不罗嗦,直接上例子(消除星星小游戏中 星星类) require "extern" StarSprite = class("StarSpr ...

  7. PAT (Basic Level) Practise:1031. 查验身份证

    [题目链接] 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9, ...

  8. jquery.cookie() 方法的使用(读取、写入、删除)

    一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. ...

  9. 在Bootstrap中 强调相关的类

    .text-muted:提示,使用浅灰色(#999) .text-primary:主要,使用蓝色(#428bca) .text-success:成功,使用浅绿色(#3c763d) .text-info ...

  10. 【NOIP2012】借教室

    因为本校OJ+1s所以用线段树水过了,不去syz的水库水这题还真不知道线段树过不了= = 原题: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的 ...