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 ...
随机推荐
- C# 序列化和反序列
1.对象序列化的介绍 (1).NET支持对象序列化的几种方式 二进制序列化:对象序列化之后是二进制形式的,通过BinaryFormatter类来实现的,这个类位于System.Runtime.Seri ...
- PHP程序漏洞产生的原因和防范方法
滥用include 1.漏洞原因: Include是编写PHP网站中最常用的函数,并且支持相对路径.有很多PHP脚本直接把某输入变量作为Include的参数,造成任意引用脚本.绝对路径泄露等漏洞.看以 ...
- CI框架篇之辅助函数篇--基本(1)
辅助函数 每个辅助函数文件仅仅是一些函数的集合URL Helpers 可以帮助我们创建链接, Form Helpers 可以帮助我们创建表单,Text Helpers 提供一系列的格式化输出方式, C ...
- WPF中窗口控件的跨线程调用
在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...
- [XML] resources的Xml配置文件 (转载)
<?xml version="1.0" encoding="utf-8" ?> <resources> <language> ...
- [Mime] MediaTypes--电子邮件类型类 (转载)
点击下载 MediaTypes.rar 这个类是关于 电子邮件类型类的操作,在发送电子邮件是规定以什么样的格式发送,Xml,HTML,文本等方式1.电子邮件类型帮助类,Xml格式,HTML格式等看下面 ...
- yieId浅谈
例子:在不使用yieId时,通常我们都会采取先遍历再把元素加到新的List中 using (var reader = SqlHelper.ExecuteReader("")) { ...
- oracle session 相关优化
导读: 同学们是不是都用遇到过这种情况,一个业务系统开发期业务并发量只是估算一个值,而系统上线后这个并发量可能会出现溢出或是不够的 情况.在这种情况下我们DBA怎么给出合理的性能优化建议呢?本文就 ...
- Css 梯形图形 并添加文字
HTML页面的代码: <body> <div style="width:500px;border:solid 1px #ccc;"> <div> ...
- C#世界中的委托
委托是C#最重要的特性之一,C#后面的所有特性基本都是建立在委托的基础上的. 1.C#委托是什么? 可以把C#的委托理解为函数的一个包装,它使得C#中的函数可以作为参数来被传递.如果你学过C++,可以 ...