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代码

CanJump.java

References:

感谢http://blog.csdn.net/fightforyourdream/article/details/14219049给予的灵感。

LeetCode: Jump Game Total 解题报告的更多相关文章

  1. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  2. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 实现iOS序列化与反序列化(runtime)

    一.变量声明 为便于下文讨论,提前创建父类Biology以及子类Person: Biology: @interface Biology : NSObject { NSInteger *_hairCou ...

  2. C语言学习笔记 (008) - C语言字符串操作总结大全(超详细)(转)

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  3. linux达人养成计划学习笔记(三)—— 帮助命令

    一.帮助命令man 1.基本使用方法: man 命令 #获取指定命令的帮助选项: -f 查看命令拥有的帮助级别 相当于whatis,也可以使用whereis来查询 -num 调用对应等级的帮助文件 - ...

  4. SharePoint 2013 Step by Step——使用自定义的List Template

    Overview 对于企业员工来说,"扁平结构"的LIST是日常操作中经常使用到的,LIST的好处是方便数据的录入以及数据的整理分析,尤其是Quick Edit功能,可以实现快速编 ...

  5. 适合移动端与PC端的 CSS Reset - m_base.css

    文章来源:http://www.cnblogs.com/HCJJ/p/6399185.html 具体代码 @charset "utf-8"; html { -ms-text-siz ...

  6. 查看mysql状态的常用命令

    在mysql客户端输入"show status"之后将会看到如下输出: 如果想要查看某个具体的值,可以使用如下命令: show status LIKE "%具体变量%&q ...

  7. C++ error: passing 'const std::map<>]' discards qualifiers或pass-by-reference-to-const-map导致的“discards qualifiers”

    产生问题的场景: int func(const map<int, string> &aMap) { string value = amap[0]; } 或者 int  Test:: ...

  8. react-native react-navigation使用

    npm install react-navigation --save 安装 代码中引入StackNavigator组件   5CF902D1-9639-494D-8775-A9A87F376734. ...

  9. NSString和NSMutablestring,copy和strong(转载)

    1.http://www.cocoachina.com/ios/20150512/11805.html 2.http://blog.csdn.net/winzlee/article/details/5 ...

  10. Linux 常见紧急情况处理方法

    使用急救盘组进行维护 急救盘组(也称为 boot/root 盘组),是系统管理员必不可少的工具.用它可以独立地启动和运行一个完整的 Linux 系统.实际上,急救盘组中的第 2 张盘上就有一个完整的 ...