DP------Jump Game
2016-01-25 17:49
211 查看
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.
Have you met this question in a real interview? Yes
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Note
This problem have two method which is Greedy and Dynamic Programming.
The time complexity of Greedy method is O(n).
The time complexity of Dynamic Programming method is O(n^2).
We manually set the small data set to allow you pass the test in both ways. This is just to let you learn how to use this problem in dynamic programming ways. If you finish it in dynamic programming ways, you can try greedy method to make it accept again.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Have you met this question in a real interview? Yes
Example
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Note
This problem have two method which is Greedy and Dynamic Programming.
The time complexity of Greedy method is O(n).
The time complexity of Dynamic Programming method is O(n^2).
We manually set the small data set to allow you pass the test in both ways. This is just to let you learn how to use this problem in dynamic programming ways. If you finish it in dynamic programming ways, you can try greedy method to make it accept again.
public class Solution { /** * @param A: A list of integers * @return: The boolean answer */ public boolean canJump(int[] A) { boolean[] can = new boolean[A.length]; can[0] = true; for (int i = 1; i < A.length; i++) { for (int j = 0; j < i; j++) { if (can[j] && j + A[j] >= i) { can[i] = true; break; } } } return can[A.length - 1]; } }
相关文章推荐
- make menuconfig 配置
- 通过线程实时获取当前时间
- 数学建模Word排版——项目编号
- Hadoop 2.6.0 FSDirectory源代码分析
- 循环队列的实现与分析,两种方法,算法导论10.1-4
- POJ 2388(快排)
- PAT A 1065. A+B and C (64bit) (20)
- 设计模式(十九):备忘录模式
- SSH无password登陆配置
- Drawable——VectorDrawable
- 回顾苹果Macintosh的进化史
- RecyclerView的使用
- 决策树(Decision Tree)
- 格式化JSON的NODE.JS代码片段
- JAVA思考题,简单的数据加密和解密
- Android 系统发送短信给多个联系人使用
- std fstream小例子
- 整理的超级全,且不断更新的机器学习、计算机视觉,自然语言处理库~
- Hello CSDN
- 修改textfield的左右视图的位置。