[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 ...
随机推荐
- Java Web 中使用ffmpeg实现视频转码、视频截图
Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html ...
- Python学习笔记八
类的高级用法 多态: 在其他语言,使用的是类的继承. 在python中,不需要指定数据类型. 基于TCP协议的socket通信实现: 类似于打电话的情景. 服务端: 1.买手机 2.插卡 3.开机 ...
- 如何破解Excel VBA密码
首先,如果文件格式是(.xslm),需要先打开Excel文件,另存为2003版格式(.xls). 然后用普通的文本编辑器(我用的是NotePad++)打开这个文件,注意文件类型选“所有文件”. 然后在 ...
- neo4j 学习笔记
1.参考 https://blog.csdn.net/appleyk/article/category/7408344 系列文章 (不支持 spring boo 2.0 以下的,入门可做参考) 2.底 ...
- Vue中父子组件通讯——组件todolist
一.todolist功能开发 <div id="root"> <div> <input type="text" v-model=& ...
- Codeforces Round #524 (Div. 2) F
题解: 首先这个东西因为强制在线区间查询 所以外面得套线段树了 然后考虑几条线段怎么判定 我们只需要按照右端点排序,然后查询的时候查找最右节点的前缀最大值就可以了 然后怎么合并子区间信息呢 (刚开始我 ...
- 修改终端terminal
修改终端路径 显示的颜色 (1)在home目录中,新建 .dircolors: $vi .dircolors 输入下面内容 DIR 00;36 保存退出. (2)在 .bashrc文件中 $vi .b ...
- require和import区别
遵循的模块化规范不一样 模块化规范:即为 JavaScript 提供一种模块编写.模块依赖和模块运行的方案.谁让最初的 JavaScript 是那么的裸奔呢——全局变量就是它的模块化规范. requi ...
- VMware workstation --虚拟机静态ip设置
背景介绍 我在本机win10上安装VMware workstation软件,新建两台centos7虚拟机,最近在配服务,每天虚拟机重启后,ip总会变,服务配置文件又要修改,很麻烦,便需要将其ip由dh ...
- [LeetCode] Similar RGB Color 相似的红绿蓝颜色
In the following, every capital letter represents some hexadecimal digit from 0 to f. The red-green- ...