1 题目

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.

接口: public boolean canJump(int[] nums);

2 思路

从数字的0位置,开始往后挑,是否能够达到最后一个元素。

思路1:一维动态规划

状态F[i],表示从第0层出发,走到A[i]时剩余的还能够走的最大步数。

F[i] < 0 :已经无法往前走了,只能够走到 i - 1层。

方程:F[i] = max(f[i - 1], A[i - 1]) - 1, i > 0

初始状态:F[0] = 0 或者 F[0] = A[0]

复杂度: Time: O(n) ; Space: O(n)

思路2:贪心思想

maxCan表示能够走到的最远的index,不断更新这个全局最值。所以,有局部最值

局部最值: maxLocal = A[i] + i,表示走到i层时,能够达到的最远的index。

复杂度: Time: O(n) ; Space: O(1)

3 代码

思路1

	public boolean canJump(int[] nums) {
int len = nums.length;
int[] f = new int[len];
f[0] = nums[0];
for (int i = 1; i < len; i++) {
f[i] = Math.max(f[i - 1], nums[i - 1]) - 1;
if (f[i] < 0)
return false;
}
return f[len - 1] >= 0 ? true : false;
}

思路2

	public boolean canJump2(int[] nums) {
int len = nums.length;
int maxCan = 0;
for (int i = 0; (i < len) && (i <= maxCan); i++) {
int maxLocal = nums[i] + i;
maxCan = maxCan > maxLocal ? maxCan : maxLocal;
if (maxCan >= len - 1) {
return true;
}
}
return false;
}

4 总结

贪心的思想,采用局部最值和全局最值,解法效率好。

DP思想,不是很好想。

疑问:DP的答案运行时间少于贪心的时间?

5 扩展

Jump Game II

6 参考

leetcode面试准备: Jump Game的更多相关文章

  1. leetcode面试准备: Jump Game II

    1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...

  2. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  3. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  4. leetcode面试准备: Game of Life

    leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...

  5. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  6. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  7. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  8. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  9. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

随机推荐

  1. Android开发之设定Dialog的位置

    今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create();                ...

  2. Linux下搭建Oracle11g RAC(3)----创建用户及配置相关文件

    配置11gR2 RAC需要安装Oracle Grid Infrastructure软件.Oracle数据库软件,其中Grid软件等同于Oracle 10g的Clusterware集群件.Oracle建 ...

  3. Android Studio: 我解决的DEX出错。

    今天开始使用了Android Studio.感觉很方便,很强大.因为它还集成了SVN,GIT等版本管理工具. 由于工程在CheckOut下来后想直接在终端上运行,在引入外部jar包之后开始运行啦,结果 ...

  4. shell中exit命令不退出脚本

    好久不用shell了,今天碰到一个坑,发现exit后,shell脚本还会运行. $JAVA_HOME/bin/jps | while read RES do PID=`echo $RES | awk ...

  5. ubuntu15.10下sublime text3 无法输入中文解决办法

    原文链接:http://geek.csdn.net/news/detail/44464 1.首先保证你的电脑有c++编译环境 如果没有,通过以下指令安装 sudo apt-get install bu ...

  6. Spring for Apache Kafka

    官方文档详见:http://docs.spring.io/spring-kafka/docs/1.0.2.RELEASE/reference/htmlsingle/ Authors Gary Russ ...

  7. idea集成svn插件

    1.需要在机器上安装一个SVN客户端命令行程序,可以到这里下载对应的安装程序:http://subversion.apache.org/packages.html#windows 我选择的是torto ...

  8. Spring 注解回顾

    [copy] 参考资料 赵蒙

  9. Js的History对象

    History回顾 window.history表示window对象的历史记录 window.history的简单回顾 历史记录中前进/后退,移动到指定历史记录点 window.history.bac ...

  10. Jquery Datatables(三)

    最近在项目中又使用Datatables的一个有趣功能,官网列子如下图: 点击“+”,展开列表,再次点击收缩. 官网的列子点击展开后的数据也是原来行中的数据,这边有了一个想法是否可以利用Ajax去动态加 ...