[LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

时间:2022-10-23 00:17:26

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. Node.js包管理器Yarn的入门介绍与安装

    FAST, RELIABLE, AND SECURE DEPENDENCY MANAGEMENT. 就在昨天, Facebook 发布了新的 node.js 包管理器 Yarn 用以替代 npm .咱 ...

  2. type

    MollyPages.org"You were wrong case.To live here is to live." Home Pages / Database / Forms ...

  3. EditText自定义边框

    1.EditText代码如下 (View代替EditText获取焦点): <View android:focusable="true" android:focusableIn ...

  4. javascript问题积累

    今天在写网页时碰到了几个js可以解决的小问题,很好用,很简便 1.鼠标移动到图片上时可更换图片,比如用到给图片加颜色,去颜色. <img src="../img/02.gif&quot ...

  5. RG100A-AA 中大校园网上网及远程配置

    由于无线网卡用得不爽,wifi经常断,所以想整个路由器,造福群众.在朋友介绍下购得一台已经刷好 Openwrt 的上海贝尔RG100A-AA路由器,根据下面的简单步骤,就能连接上校园网. 一.准备工作 ...

  6. JavaScript--模拟网络爬虫

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. javascript iframe 视频解码

    function confirmVdo(){ var videoVal = $(".video_src").val(); if(videoVal){ videoVal = vide ...

  8. 【nowcoder】 4th T2 区间

    题目链接:https://www.nowcoder.com/acm/contest/175/B 当你为时间复杂度挠头的时候 别人已经33行拿满分了 #include<cstdio> #in ...

  9. ECC

    素数 prime,又称为质数,是指,除了1和它本身,没有其他因数的数. 素数的定理: 1)在一个大于1的数a和它的2倍之间必定存在至少一个素数: 素数的性质: 1)在所有的大于10的质数中,个位数,只 ...

  10. system&period;setProperties

    System.setProperty("http.proxyHost", "localhost");System.setProperty("http. ...