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 ...
随机推荐
- >/dev/null 2>&1的含义
转帖:http://www.cnblogs.com/dkblog/archive/2009/07/31/1980722.html os.system("/etc/init.d/winbind ...
- Swift 下标脚本
前言 在访问一个数组实例的元素时,可以使用 Array[index] 的形式.在访问一个字典实例的元素时,可以使用 Dictionary[index] 的形式.这种方括号的形式就是 "下标脚 ...
- Git 查看提交历史(分布式版本控制系统)
1.查看提交历史 在提交了若干更新,又或者克隆了某个项目之后,你也许想回顾下提交历史.完成这个任务最简单而又有效的工具是 git log 命令. $ git log commit ca82a6dff8 ...
- OpenCV 学习笔记03 直线和圆检测
检测边缘和轮廓不仅重要,还经常用到,它们也是构成其他复杂操作的基础. 直线和形状检测与边缘和轮廓检测有密切的关系. 霍夫hough 变换是直线和形状检测背后的理论基础.霍夫变化是基于极坐标和向量开展的 ...
- win7 32位 安装opencv-python后,运行时提示 "from .cv2 import *: DLL load failed: 找不到指定的模块" 的解决办法
安装opencv后,运行一个测试程序提示"from .cv2 import *: DLL load failed: 找不到指定的模块".于是百度一下解决办法,结果试了N多方法后也没 ...
- thinkphp __hash__
thinkphp会自动在模板文件的表单后加一个__hash__来验证提交,保证一定程序上的安全: 但是有时用不着,可以用: C('TOKEN_ON',false); 关闭这项
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
一般为AndroidManifest.xml中的activity的问题 这次主要是因为activity在另外一个package里面,但是package名称第一次字母是大写,所以出错 把package名 ...
- LINUX-iostat命令讲解
语法如下:iostat [ -c | -d ] [ -k ] [ -t ] [ -V ] [ -x [ device ] ] [ interval [ count ] ]-c为汇报CPU的使用情况:- ...
- 两种方法实现js页面隔几秒后跳转,及区别
这里需要用到window的两个对象方法,setInterval()和setTimeout() 一. 区别: 1. setInterval(code,millisec) 周期性(millisec单位 ...
- [转]FutureTask详解
FutureTask类是Future 的一个实现,并实现了Runnable,所以可通过Excutor(线程池) 来执行,也可传递给Thread对象执行.如果在主线程中需要执行比较耗时的操作时,但又不 ...