[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 ...
随机推荐
- python的单、双、多分支流程控制
if流程控制总结: 1.当满足条件时,执行满足条件的代码. 2.当执行完if语句内代码,程序继续往下执行. 单分支: if 条件成立,执行满足条件的代码 如下: if a>50: print(' ...
- 蓝桥杯 倍数问题(dfs,枚举组合数)
标题:倍数问题 [题目描述]众所周知,小葱同学擅长计算,尤其擅长计算一个数是否是另外一个数的倍数.但小葱只擅长两个数的情况,当有很多个数之后就会比较苦恼.现在小葱给了你 n 个数,希望你从这 n 个数 ...
- web服务-2、四种方法实现并发服务器-多线程,多进程,协程,(单进程-单线程-非堵塞)
知识点:1.使用多线程,多进程,协程完成web并发服务器 2.单进程-单线程-非堵塞也可以实现并发服务器 1.多进程和协程的代码在下面注释掉的部分,我把三种写在一起了 import socket im ...
- 用户管理和su,id 命令
useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...
- ETL测试教程
在我们了解ETL测试之前,先了解有关商业智能和数据仓库的重要性. 让我们开始吧 - 什么是BI? 商业智能是收集原始数据或业务数据并将其转化为有用和更有意义的信息的过程. 原始数据是一个组织每日事务的 ...
- [PA2014]Matryca
[PA2014]Matryca 题目大意: 有一堵长度为\(n(n\le10^6)\)的墙需要刷漆,你有一把长度为\(k\)的刷子.墙和刷子都被均匀划分成单位长度的小格,刷子的每一格中都沾有某种颜色的 ...
- Ubuntu安装VLC播放器
Ubuntu安装VLC官方介绍:http://www.videolan.org/vlc/download-ubuntu.html sudo apt-get update sudo apt-get in ...
- node.js官方文档解析 02—buffer 缓冲器
Buffer 类的实例类似于整数数组,但 Buffer 的大小是固定的.且在 V8 堆外分配物理内存.Buffer 的大小在被创建时确定,且无法调整. Buffer 类在 Node.js 中是一个全局 ...
- mac上adb command not found
第一种报错(使用的自带mac命令行) bash: adb: command not found 1.vim ~/.bash_profile ,如果.bash_profile不存在,先touch ~/. ...
- Windows 2012 下Redmine安装和环境搭建
公司在过去一年中处于高速发展创业期,对于技术管理和项目管理没有找到一个很好的管理工具,使用过Teanbition+禅道+SVM的集成管理工具,但是明显各工具之间联系性差,断层严重,不能很好的形成团队成 ...