[LeetCode] 55. Jump Game_ Medium 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.
Determine if you are able to reach the last index.
Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
jump length is 0, which makes it impossible to reach the last index. 这个题目因为是问的yes/no,然后跟坐标有关(也就是说如果数组里面有数字互相调换了答案就不一样了),很有可能是dynamic programming,这里用动态规划来做。f(i) 表示能否跳到该点,function 用
f(i) = true if f(j) and nums[j] >= i - j (for loop), 一旦有就break,这里可以稍微improve一点时间上的效率。
T: O(n^2) S: O(n)
note:但是在leetcode上面这个会limit time exceed。 Code:
class Solution:
def jumpGame(self, nums):
if not nums: return False
n = len(nums)
mem = [False] * n
mem[0] = True
for i in range(1, n):
for j in range(0, i):
if mem[j] and nums[j] >= i - j:
mem[i] = True
break
return mem[n - 1]
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming的更多相关文章
- [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] 62. Unique Paths_ 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] 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] 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 ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- lxml.etree.HTML(text) 解析HTML文档
0.参考 http://lxml.de/tutorial.html#the-xml-function There is also a corresponding function HTML() for ...
- mysql安装运行(centos)
http://repo.mysql.com寻找需要的版本 wget -P /opt/downloads http://repo.mysql.com/mysql57-community-release- ...
- thinkphp的静态缓存,数据缓存,快速缓存,查询缓存
// 静态缓存 // 'HTML_PATH' 缓存目录,这是个常量不是配置项,在入口文件中定义 // 'HTML_CACHE_ON' => true, // 开启静态缓存 'HTM ...
- eclipse 下载安装单元测试log4j的配置与搭建
搭建log4j的具体步骤 1.下载jar包放在lib 拓展库中 百度云下载 地址 链接:https://pan.baidu.com/s/1Cvb22kCJcymORehwhKnCrQ 提取码:b55m ...
- MFC开发(一)简单同步时间应用程序
看了一个垃圾程序的架构,mmp真坑,自己费了一点功夫才搞定,就直接记录下吧,这个是windows简单的应用程序,但是里面有点复杂,我们需要首先建立一个基于mfc的appwinzard程序,(凭记忆写的 ...
- LBS(Location Based Service)(基于位置的服务)
LBS(Location Based Service)(基于位置的服务) Android 中定位方式基本可以分为两种:GPS定位,网络定位. GPS定位的工作原理是基于手机内置的GPS硬件直接和卫星进 ...
- Python数据分析基础教程
Python数据分析基础教程(第2版)(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1_FsReTBCaL_PzKhM0o6l0g 提取码:nkhw 复制这段内容后 ...
- 文件上传(xls)
function UploadFile(){ var filewj =document.getElementById("filewj").files[0]; //input Id ...
- Oracle命令行中显示:ORA-04076: 无效的 NEW 或 OLD 说明
Oracle命令行进行操作时可能出现"ORA-04076: 无效的 NEW 或 OLD 说明" 需要在条件语句中JOB前面添加“old.”即可(因为是在when条件里面,所以不用“ ...
- NOIP-玩具谜题
题目描述 小南有一套可爱的玩具小人,它们各有不同的职业. 有一天,这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外,如下图: 这时 `singer` 告 ...