leetcode面试准备: Jump Game
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]
, returntrue
.
A =[3,2,1,0,4]
, returnfalse
.
接口: 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
- Code Ganker征服代码
leetcode面试准备: Jump Game的更多相关文章
- leetcode面试准备: Jump Game II
1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- leetcode面试准备:Add and Search Word - Data structure design
leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- leetcode面试准备:Implement Trie (Prefix Tree)
leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
随机推荐
- linux中的网络通信指令 分类: 学习笔记 linux ubuntu 2015-07-06 16:02 134人阅读 评论(0) 收藏
1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...
- oracle用户管理实例
oracle中的用户角色分为预定义角色和自定义角色. 角色是把常用的权限集中起来形成角色. 授权/分配角色命令 grant 权限/角色 to 用户 收回权限命令: revoke 综合案例: 创建一个用 ...
- Java基础知识强化之集合框架笔记56:Map集合之HashMap集合(HashMap<String,Student>)的案例
1. HashMap集合(HashMap<String,Student>)的案例 HashMap是最常用的Map集合,它的键值对在存储时要根据键的哈希码来确定值放在哪里. HashMap的 ...
- GUI编程笔记(java)06:GUI窗体添加按钮并对按钮添加事件案例
1.需求:把按钮添加到窗体,并对按钮添加一个点击事件. 步骤: (1)创建窗体对象(2)创建按钮对象(3)把按钮添加到窗体(4)窗体显示 2.编写程序思路: 窗体布局:窗体中组件的排列方式 布局分类 ...
- Eclipse - 添加 PyDev 插件
1. 安装PyDev插件 启用Eclipse.在Help菜单中,选择Install New Software···, 然后点击Add按钮.在Location中输入:http://pydev.org/u ...
- struts2 自定义拦截,防止非法操作
<package name="defaults" extends="struts-default"> <interceptors> &l ...
- XPath操作XML文档
NET框架下的Sytem.Xml.XPath命名空间提供了一系列的类,允许应用XPath数据模式查询和展示XML文档数据. 3.1XPath介绍 主要的目的是在xml1.0和1.1文档节点树种定位节点 ...
- 安卓Intent(显式)
1.Intent是Android程序中各组件之间交互的重要方式,一般可用于启动活动.启动服务.以及发送广播等场景,这里先对活动进行说明Intent的一些作用. 2.Intent的用法大致可分为,显式I ...
- (转)JS获取当前对象大小以及屏幕分辨率等
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- .NET设计模式(4):建造者模式(Builder Pattern)
):建造者模式(Builder Pattern) .建造者模式的使用使得产品的内部表象可以独立的变化.使用建造者模式可以使客户端不必知道产品内部组成的细节. 2.每一个Builder都相对独立, ...