[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 ...
随机推荐
- Win7 安装bundle
bundle依赖ruby,因此需要下载并安装一下内容: 1. rubyinstaller 这个是windows专用的ruby安装程序,下载地址是http://rubyinstaller.org/ 2. ...
- mysql查看索引与锁
http://www.cnblogs.com/cocos/archive/2011/05/06/2039428.html Mysql乐观锁与悲观锁 http://www.cnblogs.com/esi ...
- django---不使用view,直接从Url转到html
这个在使用公告页时,就很方便. 因为无需要经过数据库,视图. 直接使用文字. https://docs.djangoproject.com/en/2.1/topics/class-based-view ...
- flask狗书
Models.py #coding:utf8 fromflaskimportFlask fromflask_sqlalchemyimportSQLAlchemy app=Flask(__name__) ...
- vscode断点调试工程化服务端文件
一.创建express应用我们使用express-generator创建一个新的express应用.1.全局安装express-generator // 安装 sudo npm install exp ...
- JAVA基础复习与总结<一> 对象与类的概念_内部类_继承与多态
一.对象与类 类:类是一个模版,它描述了一类对象的行为和状态. class animal { private int color; private int size; public void eat ...
- 04-Python入门学习-流程控制
一.流程控制if 语法1: if 条件: code1 code2 code3 .... age=180 height=163 weight=75 sex='female' is_beautif ...
- 13树莓派手动安装Home Assistant
2017-09-05 00:53:02 https://home-assistant.io/docs/installation/raspberry-pi/ 已经安装步骤安装了带桌面的树莓派系统,在SD ...
- c#关键字和常用类型表快查
类型 字节 取值范围 说明 bool 1 true/false/null 布尔类型 char 2 0x0000~0xffff Unicode 16 位字符 byte 1 0~255 无符号的 8 位整 ...
- PHP 根据子ID递归获取父级ID,实现逐级分类导航效果
代码: //当前路径 $cate=M('wangpan_class')->select(); function get_top_parentid($cate,$id){ $arr=array() ...