[LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [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 ...
随机推荐
- java 小程序开发PKCS7Padding 解密方法实现,以及错误Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
近日在对接小程序API,其中wx.getUserInfo api返回的数据encryptedData 的解密算法要求为: AES-128-CBC,数据采用PKCS#7填充. 经过一番查询,得到java ...
- C#软件监控外部程序运行状态
需要外挂一个程序,用于监控另一个程序运行状态,一旦检测到另一程序关闭,就触发一个事件做其他处理. 引用的类 1 using System.Diagnostics;//引入Process 类 声明 1 ...
- 使用apt-get安装相关的软件时,不能Fetch,现在更新为国内的源!
我使用的是中国科技大学的树莓派的软件源,测试可以使用(更新时间:2018年7月15日) deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ jessie ...
- Round#534 div.2-C Grid game
http://codeforces.com/contest/1104/problem/C 好厉害的题~ 只要把竖着的放在第一第二行,横着的放在第三/第四行就行. 哦吼,大半夜脑子迷糊地看英文的脑筋急转 ...
- 详述 hosts 文件的作用及修改 hosts 文件的方法
1 什么是hosts文件? hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的 IP 地址建立一个关联“ 数据库 ”.当用户在浏览器中输入一个需要登录的网址时,系统会首 ...
- ie下js不执行的几种可能
1.首先考虑的就是代码是否有报错2.其次搜索代码中是否有console.log等测试的代码3.检查所用的方法是否兼容ie 以上是鄙人在开发过程中遇到的几种情况,抛砖来引玉~~
- [LeetCode] Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- 解密Redis的持久化和主从复制机制
Redis持久化 Redis 提供了多种不同级别的持久化方式: RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器执 ...
- linux安装杀毒软件
https://www.cnblogs.com/bingo1024/p/9018212.html
- 内置对象之request对象
内置对象就是(容器)已经创建好的对象,可以被直接使用.当用户发送一个请求给容器,它就会自动创建一个对象来处理客户端发送来的消息,如request这个对象,可以获取到用户(客户端)发送来的信息.它的常见 ...