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. 利用远程服务器在docker容器搭建pyspider运行时出错的问题

    This system supports the C.UTF-8 locale which is recommended. You might be able to resolve your issu ...

  2. 前端js总结

    1 . 在controller层中的@ResponseBody注解中返回的要是一个对象而不能用字符串. 2 . 给html页面的按钮添加单击事件 $(#login).click( function l ...

  3. AWS S3 递归上传文件和递归下载文件, 以及S3之间拷贝文件夹

    1. 递归上传文件: aws s3 cp 本地文件夹 s3://bucket-name -- recursive --region us-east-1 2. 递归下载S3上的文件夹: cd  本地下载 ...

  4. 【Linux】Linux简介

    思维导图 什么是Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统. Linux能运行主要的UNIX工 ...

  5. npm的一些常用命令(在国内,建议使用cnpm,在淘宝镜像里面下载就行)

    npm的一些常用命令(在国内,npm操作可能会比较慢,建议使用cnpm,在淘宝镜像里面下载就行,用法和npm一样) cnpm安装地址,直接安装 npm install cnpm -g --regist ...

  6. Python开发实战PDF

    Python开发实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1iP9VmwuzDMfdZTfpupR3CA 提取码:a523 复制这段内容后打开百度网盘手机A ...

  7. Python assert 断言函数

    http://blog.csdn.net/hunyxv/article/details/52737339 使用assert断言是学习python一个非常好的习惯,python assert 断言句语格 ...

  8. 05-Python入门学习-字符串与列表的内置方法

    字符串 一:基本使用 1 用途: 记录描述性的状态,比如人的名字.地址.性别 2 定义方式: 在"",'',"""""" ...

  9. React(二)组件

    1.组件概念: 我理解的组件的概念就是复用性,一个组件开发完成后可以重复使用. 2.简单的组件编写 (1)在src中创建一个components的文件夹,里面创建一个header.js的文件 (2)在 ...

  10. [LeetCode] Chalkboard XOR Game 黑板亦或游戏

    We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu ...