57. Jump Game && Jump Game II
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的更多相关文章
- [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 ...
- BestCoder27 1001.Jump and Jump... (hdu 5162) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5162 题目意思:有 n 个 kid,每个 kid 有三个成绩 a, b, c.选最大的一个成绩作为这个 ...
- 杭电oj 1087——super jump!jump!jump(java实现)
question:Super Jumping! Jumping! Jumping! 意思就是找一串数字中的和最大子串 思路:创建另一个数组,每一项是路径数组对应项之前最大子串的和,然后遍历此数组找出最 ...
- Solution -「CodeChef JUMP」Jump Mission
\(\mathcal{Description}\) Link. 有 \(n\) 个编号 \(1\sim n\) 的格子排成一排,并有三个权值序列 \(\{a_n\},\{h_n\},\{p_n ...
- Jump Game II 解答
Question Given an array of non-negative integers, you are initially positioned at the first index of ...
- 2018今日头条杯 E-Jump a Jump
Problem E. Jump A JumpInput file: standard inputOutput file: standard outputTime limit: 1 secondsMemor ...
- LeedCde 题解目录
1. Longest Palindromic Substring ( 最长回文子串 ) 2. Median of Two Sorted Arrays (两个排序数组的中位数) 3. Sqrt(x) 4 ...
- 《CODE》读后笔记——第14~20章
14.反馈与触发器 振荡器不需要人的干涉即可自主且不断地实现断开和闭合.所有计算机都靠某种振荡器来使其他部件同步工作. 当两个开关都断开时,电路有两个稳定状态,这样的一个电路称为触发器.触发器具有记忆 ...
- TopCoder SRM 633div1
250pts PeriodicJumping 题意:从起点开始,每次按找数组jump给定的长度,即jump[0], jump[1], jump[2].....jump[n-1], 向各个方向跳,跳 ...
随机推荐
- movebase导航
利用turtlebot 的导航配置文件 由于movbase发的速度太不友好了所以使用了ros自带的滤波安装相应的包 apt-get install ros-indigo-yocs-velocity-s ...
- 局域网单机部署双tomcat内外网不能访问防火墙问题查出来
tomcat部署的项目内网访问不了 win7 64 位 控制面板 - 安全 -防火墙-入站规则- 新建规则 选中“端口”按钮,点选“下一步”: 选择“TCP”按钮,在“特定本地端口”输入tomcat ...
- c/c++面试题(5)(c++重要的概念详解)
1.C++面向对象的三大特征? 1)封装:将客观事物封装成抽象的类,并且设计者可以对类的成员进行访问控制权限控制. 这样一方面可以做到数据的隐藏,保护数据安全;另一方面,封装可以修改类的内部 实现而不 ...
- Android平板电脑开发— — —碎片
碎片是一种可以嵌入在活动中的UI片段,它能让程序更加合理与充分地使用大屏幕的空间,碎片通常都是在平板电脑开发中才会使用 简单实例 左碎片布局 <?xml version="1.0&qu ...
- CSS 第四天 多重背景 变形 过渡
背景原点:background-origin 图片起始位置 border-box包括边框 padding-box边框内 content-box 内容内 **background-repeat 为no- ...
- innerHTML,innerText,outHTML,outText区别
<p><div id="div" style="background-color:#ff9966;border:1px #ff0000 dashed;& ...
- Android 优化List图片显示
通常在界面中涉及到大量图片加载的时候都会产生卡顿,因此需要优化 其核心思想就是减少在getView()中的代码量和操作,让其尽可能的轻量化.众多方法最根本的目的是 将一切耗时的操作从getView中抽 ...
- PL/SQL工具连接ORALCE数据库的方法
http://www.cnblogs.com/dongzhiquan/archive/2011/11/21/2257629.html 1.利用ORACLE NET MANAGER工具 1)打开 ORA ...
- 网页闯关游戏(riddle webgame)--游戏玩法和整体介绍
前言: 记得上大学那会, 有位传说中的大牛, 写了一个网页闯关类的游戏. 当时我们玩得不亦乐乎, 也是第一次接触到这种形式的游戏. 不过当时纯玩家心态, 并没有想过去创造一个. 最近想起这事, 突然想 ...
- MetaPhlAn 2:宏基因组进化分析
描述 MetaPhlAn是分析从物种水平分辨率宏基因组鸟枪法测序数据的微生物群落(细菌,古细菌,真核细胞和病毒)的组成的计算工具.从版本2.0,MetaPhlAn还能够确定具体的菌株(在将样品含有先前 ...