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. Python初学记录

    发音: 拍怂 语系:类C 特点: 1语句控制不用{}和(),而是强制用户空格或tab缩进.空格和tab数量不一定. 2解释性语言,不需要事先声明变量,即写即用. 3.list 列表可存放多种类型数据. ...

  2. EnumWindows 使用

    转载自csdn:http://blog.csdn.net/hairi/article/details/548064   EnumWindows 用来列举屏幕上所有顶层窗口. MSDN原话: The E ...

  3. EF收集

    http://www.cnblogs.com/end/archive/2011/08/18/2144250.html http://www.cnblogs.com/zzdfc/archive/2009 ...

  4. GCDTimer

    #import <Foundation/Foundation.h> @interface JKTimerManager : NSObject + (instancetype)sharedT ...

  5. sql server获取当前日期

    SqlServer中得到当前日期(convert函数,getdate函数)函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3. ...

  6. 217. Contains Duplicate(C++)

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  7. Angularjs中编写指令模版

    angular.module('moduleName', []).directive( 'namespaceDirectiveName', [ function() { return { restri ...

  8. 原生js判断是否有某个class,如果有就删掉,没有加上

    <style> #div1 { width: 100px; height: 100px; position: absolute; } .div1 { background: red; } ...

  9. CentOS6.5 编译安装lnmp环境

    参考:http://54im.com/tag/libmcrypt http://www.educity.cn/linux/1240338.html 设置防火墙,并开启3306 80端口:vi /etc ...

  10. php生成员工编号,产品编号

    由于某些原因需要获取数据库最大的id值.所以出现了这段php 获取数据库最大的id代码了.这里面的max(id) 这里面的id 就是要获取最大的id了.如果是别的字段请填写为其他字段 获取数据库中最大 ...