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.

Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:

You can assume that you can always reach the last index.

这个题目思路跟[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming思路很像,只不过用f(i) 来表示到目前的最少的步数。f(i) = f(j) + 1 if f(j) > - 1 and nums[j] >= i - j.

Note: 同样这个题目也是time limit exceed。

Code:

class Solution:
def jumpGame2(self, nums):
n = len(nums)
mem = [-1] * n
mem[0] = 0
for i in range(1, n):
for j in range(0, i):
if mem[j] > -1 and nums[j] >= i - j:
mem[i] = mem[j] + 1
break
return mem[n - 1]

[LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  4. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  5. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  6. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  9. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. [原创]基于Zynq Linux环境搭建(三)

    此篇编译Kernel 解压: [#17#17:26:56 FPGADeveloper@ubuntu ~/Zybo_Demo]$tar zxvf *.tar.gz 进入到文件kernel文件夹中,cle ...

  2. PHP二维数组按某个字段排序

    //准备 二维数组 //按一个字段排序 foreach($rank as $key=>$val){ $dos[$key] = $val['timelength']; } array_multis ...

  3. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

    0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...

  4. 关于hbase api的个人总结(带jar包)

    1.如果本机代码没问题,jar包没问题的话,检查集群也没用问题,但是代码运行显示 找不到对应的映射主机,需要在  C:\Windows\System32\drivers\etc  中修改host文件, ...

  5. dagger2 依赖注入

    前言: 当 mvp + dagger2 + rxjava 三者遇上,架构更加清晰,代码更加灵活,巧妙结合. 依赖注入:是为了解耦,达到高内聚低耦合的目的,保证代码的健壮性.灵活性和可维护性. publ ...

  6. c#常用数值范围汇总

    short.MaxValue 32767 short.MinValue -32768 int.MaxValue 2147483647 int.MinValue -2147483648 long.Max ...

  7. winform 利用Http向服务器上传与下载文件

    利用在服务器端的IIS,布置“请求处理映射”.从而处理,本地发出Post请求.Url指向web网站所在路径的请求映射.由映射代码实现服务器保存文件. winform里面使用,WebClient的对象, ...

  8. [LeetCode] Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  9. ubuntu16.04中supervisor安装与简单使用(转载)

    ubuntu16.04中supervisor安装与简单使用 supervisor 进程管理是可以让进程在后台运行,而不占用控制台影响使用 1. 安装 supervisor sudo apt insta ...

  10. 基于STM32的USB枚举过程学习笔记(转)

    之前使用ST官方的库以及网络的资料,完成了使用USB HID类进行STM32和PC机的通讯.由于其他原因并没有深入的分析,虽然实现了功能,但是关于USB设备的枚举,以及具体的通讯方式都没有清晰的概念, ...