LeetCode: Jump Game Total 解题报告
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.
Solutions:
1. DP1
每到一个点 i,我们扫描之前所有的点,如果之前某点j本身可达,并且与current 点可达,表示点i是可达的。
返回值:DP数组的最后一个值。
// DP1.
public boolean canJump1(int[] A) {
if (A == null || A.length == 0) {
return false;
} int len = A.length;
boolean[] can = new boolean[len];
can[0] = true; for (int i = 1; i < len; i++) {
can[i] = false;
for (int j = 0; j < i; j++) {
// j can arrive and can jump to i.
if (can[j] && A[j] >= i - j) {
can[i] = true;
break;
}
}
} return can[len - 1];
}
2. DP2
优化的点1:既然某点可达,肯定前面的点全部是可达的。这个比较好理解。因为你到达i点肯定要经过前面的一个点,这个依次推知可知前面所有的点可达。
所以我们不需要数组来记录结果,只要默认i点前全部可达就行了。
优化点2:如果某点不可达了,直接返回false。不需要再往后计算。
返回值:如果全部扫完仍没有返回false,返回true。
// DP2.
public boolean canJump2(int[] A) {
if (A == null || A.length == 0) {
return false;
} int len = A.length; for (int i = 1; i < len; i++) {
boolean can = false;
for (int j = 0; j < i; j++) {
// j can arrive and can jump to i.
if (A[j] >= i - j) {
// 说明i是可达的,置标记位
can = true;
break;
}
} // 优化:如果某一步已经到不了了,后面的也不必再计算了.
if (!can) {
return false;
}
} return true;
}
3. 递归
思想是,从前至尾扫描,至第一个距离与本点可达的点j,计算j点是否可达。使用递归计算j
点的可达性。
其实这里还是用到了贪心的思维。在考虑本点是否可达的时候,我们是考虑与本点最远的一个点是否可达。实际上这也make sense。
假设j点可以到达i点,那么后面的点可以不管。
(1)因为如果j点不可达,那么j+1也不可达。如果i不可达,后面的点也可不算。
(2)如果j点可达,并且j点可到达i,那么也没有必要再往后计算了。因为结论已经出来了。
(3) 如果j点可达,但j不可达i,那么就继续计算。
// 3. DFS.
public static boolean canJump3(int[] A) {
if (A == null || A.length == 0) {
return false;
} return canJump(A, A.length - 1);
} public static boolean canJump(int[] A, int index) {
if (index == 0) {
return true;
} for (int i = 0; i <= index - 1; i++) {
if (A[i] >= index - i) {
return canJump(A, i);
}
} return false;
}
4. 贪心法(2015.1.13 redo)
Leetcode加强了test case,用动规现在是过不了的。我们现在来使用贪心法One pass解决此问题。
维护一个right (表示右边能跳到的最远的点),从左往右扫描,根据当前可跳的步骤不断更新right ,当right到达终点,即可返回true. 若更新完right后,right未
动,并且index = right,而且这时没到达终点,代表我们不可能到达终点了。(当前index的可跳值应该是0)。
// solution 3: one pass.
public boolean canJump(int[] A) {
// 4:42
if (A == null) {
return false;
} int len = A.length; int right = ;
for (int i = ; i < A.length; i++) {
right = Math.max(right, i + A[i]);
if (right == len - ) {
return true;
} if (i == right) {
return false;
}
} return true;
}
5. GitHub代码
References:
感谢http://blog.csdn.net/fightforyourdream/article/details/14219049给予的灵感。
LeetCode: Jump Game Total 解题报告的更多相关文章
- LeetCode: Jump Game II 解题报告
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- BAT面试的准备—iOS篇
本文主要用于记录在准备BAT面试中关于iOS遇到的问题和做一些相关面试题的笔记 iOS网络层设计 1.网络层和业务层的对接设计 使用哪种交互模式来和业务层对接 : 使用Delegate为主,目的是为了 ...
- 【LeetCode】200. Number of Islands (2 solutions)
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- IT技术需求建立时需考虑的因素
2012-11-13 内容存档在evernote,笔记名"IT技术需求建立时需考虑的因素"
- CentOS 开启防火墙 firewall ,mysql 远程访问
最近在阿里云服务器centos上安装了mysql数据库,默认是不开启远端访问功能,需要设置一下防火墙,在开放默认端口号 3306时提示FirewallD is not running,经过排查发现是防 ...
- ASP.NET MVC 向浏览器发送文件以提供文件下载功能
撑到大三了,结果发现周围的同学更加堕落了,尤其是某些人,表面上看起来很认真,实际上三天打鱼,两天晒网,结果一事无成,却还要抱怨学校教育失败. 为了吸取他们的教训,就算是一个小小的编码问题,我也要努力解 ...
- 【转】AlphaGo与人工智能
AlphaGo与人工智能 在之前的一篇文章中我指出,自动驾驶所需要的“视觉识别能力”和“常识判断能力”,对于机器来说是非常困难的问题.至今没有任何机器可以在视觉方面达到驴的水平,更不要说和人比.可是最 ...
- ldd命令
ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库. 参数说明: --version 打印ldd的版本号 -v --verbose 打印所有信息,例如包括符号的版本信息 -d --d ...
- python groupby 函数 as_index
在官方网站中对as_index有以下介绍: as_index : boolean, default True For aggregated output, return object with gro ...
- Knockout: 实践CSS绑定和jQuery的blur失去焦点事件, 给未通过校验的输入框添加红色边框突出显示.
目的: 实践一下Knockout提供的CSS绑定功能和JQuery的blur失去焦点事件, 这次不使用Knockout的afterkeydown事件了. 步骤: 先在htm中添加.error的css样 ...
- Mysql 导入CSV数据 语句 导入时出现乱码的解决方案
1. 登陆mysql 2. use testdb 3. 执行导入语句 LOAD DATA LOCAL INFILE 'd://exportedtest2.csv' INTO TABLE usertab ...