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. VC++/MFC操作ini配置文件详解

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...

  2. GUI编程笔记(java)08:GUI通过鼠标移动到按钮上更改背景色案例

    首先我们看看源代码如下: package cn.itcast_06; import java.awt.Button; import java.awt.Color; import java.awt.Fl ...

  3. JavaScript的DOM操作(二)

    一:window.history对象 历史记录,通过历史记录可以操作页面前进或者后退 window.history.back();后退 window.history.forward();前进 wind ...

  4. VS2010调试多进程--医疗His调试中使用

    有些时候碰到自己开发的程序嵌入到别人的框架中,而在接口的地方出了问题,而又不方便将自己的模快加入到别人的工程中这个时候VS的附加到进程调试变得不可或缺了,直奔主题 http://msdn.micros ...

  5. memcached 安装

    安装 memcached 需要 三部1,下载 memcached 放到php目录将php_memcached.dll 放到php的ext 目录 2,打开管理员命令,将memcached 拖拉到命令中, ...

  6. Quarzt.NET的Cron表达式理解

    网上关于Quarzt.NET的Cron表达式介绍有很多,但都是基本的语法,稍微深入一些的就没有了. 基本语法介绍请参看: http://www.cnblogs.com/lzrabbit/archive ...

  7. C#设置与获取目录权限(.net控制ACL)

    找到两种方式可以修改文件夹的权限 第一种: 想用c#来设置和读取ntfs分区上的目录权限,找了很多资料,未果.终于发现了一段vb.net的代码,做了修改,以C#展示给大家. using System; ...

  8. javascript创建对象(一)

    对象定义:无序属性的集合,属性包含基本值.对象.函数,相当于一组没有特定顺序的值.    创建自定义对象最简单的方式就是: var movie=new Object(); movie.name=&qu ...

  9. (九)Struts2 防重复提交

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:重复提交示例演示 struts.xml <?xml version ...

  10. iOS消息推送机制

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...