[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 ...
随机推荐
- 关于saltstack
配置Saltstack master 服务器 master服务器:saltstack.master 172.18.1.103 minion客户端:minion01 172. ...
- Java Web项目漏洞:检测到目标URL存在http host头攻击漏洞解决办法
检测到目标URL存在http host头攻击漏洞 详细描述 为了方便的获得网站域名,开发人员一般依赖于HTTP Host header.例如,在php里用_SERVER["HTTP_HOST ...
- SpringBoot整合Dubbo,并实现dubbo实现动态调用
在一些业务场景中,CP定单提交过来,需要提交到不同的通道进行业务处理 本文通过Dubbo以定义一个interface,各个通道方来实现这个接口.通过group来区分不同的通道 有需要的同学可以下载 示 ...
- scrapy_redis 相关: 查看保存的数据
0.参考资料 https://redis.io/topics/data-types-intro An introduction to Redis data types and abstractions ...
- 2.使用RNN做诗歌生成
诗歌生成比分类问题要稍微麻烦一些,而且第一次使用RNN做文本方面的问题,还是有很多概念性的东西~~~ 数据下载: 链接:https://pan.baidu.com/s/1uCDup7U5rGuIlIb ...
- c++中字符串的反转
1.对于用char定义的字符串:使用string.h中的strrev函数 #include <iostream> #include <cstring> using namesp ...
- idea报错:Invalid bound statement (not found)
在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...
- Chapter 3 Introduction to Objects and Input/Output
与声明一个primitive variable不同,声明一个对象的时候,并不创建用来存储一个对象的内存空间,而是创建了一个存储该对象所在内存空间的地址. 在java里,new是一个操作符,它让系统分配 ...
- Java虚拟机----垃圾回收与内存分配
一.垃圾回收的对象: 在Java的运行时数据区中,程序计数器和虚拟机栈.本地方法栈是随着线程的生灭而生灭,栈当中栈帧的大小在编译的时候已知,在方法结束之后栈帧出栈,这部分的垃圾回收是明确的,因此需要讨 ...
- CSS3_过渡_2D 变换_瓶体旋转_动态时钟
1. 过渡 transition 允许 CSS 的属性值在一定时间内平滑的过渡, 在鼠标点击,鼠标滑过或对属性改变中触发,并圆滑的改变 CSS 的属性值 简写属性: #box { width: 300 ...