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. Python 中的__new__和__init__的区别

    [同] 二者均是Python面向对象语言中的函数,__new__比较少用,__init__则用的比较多. [异] __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例对象,是 ...

  2. 【Spring】Spring之向 IOC 容器注入对象的三种方式

    关于Spring的搭建可参见:浅析Spring框架的搭建.在测试之前还是应该先将环境配置好,将相关Jar包导进来.Spring创建的对象,默认情况下都是单例模式,除非通过scope指定. 向IOC容器 ...

  3. C#项目中引入app.manifest管理员权限运行

    原文地址:https://blog.csdn.net/qq395537505/article/details/51010962 参考UAC:User Account Control 参考IE保护模式: ...

  4. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  5. 转 linux进程内存到底怎么看 剖析top命令显示的VIRT RES SHR值

    引 言: top命令作为Linux下最常用的性能分析工具之一,可以监控.收集进程的CPU.IO.内存使用情况.比如我们可以通过top命令获得一个进程使用了多少虚拟内存(VIRT).物理内存(RES). ...

  6. Android长度单位详解(dp、sp、px、in、pt、mm、dip)

    Android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...

  7. linux 查看 cpu个数 核心数 线程数

    深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/43935535 (1).查看cpu信息 [root@xckydb ~]# cat ...

  8. Mac下命令行下载android源代码并构建apk过程

    前提是java .sdk.ndk .cmake.gradle .gradlew都已经安装和配置好. 1.下载源码: git clone http://git-ma.xxxx.com.cn/xxxx/x ...

  9. 使用maven生成可执行的jar包

    从pom的xsi中可以打开描述pom的schema: 可以看到pom中,project的结构: 默认的mvn install生成的jar是不带主类入口的,需要在maven-compile-plugin ...

  10. hive splict, explode, lateral view, concat_ws

    hive> create table arrays (x array<string>) > row format delimited fields terminated by ...