Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is [2, 3, 7, 101]
, therefore the length is 4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
用DP的方法,每一个dp[i]代表从nums[0]到这个元素nums[i]的最长的inreasing subsequence的长度。O(N^2)的解法:
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
n = len(nums)
dp = [1] * n for i in range(0, n):
for j in range(i+1, n):
if nums[j] > nums[i]:
dp[j] = max(dp[i] + 1, dp[j]) return max(dp)
解法二 O(N logN)
本题可以用一个dp list来维护LIS的solution。对于nums里面的数跑一个for loop。如果遇到dp = [],需要把当前的num填到dp list里面。如果发现num大于dp list的最后(也就是最大的那个数)就append上去。如果num在[dp[0], dp[-1]]区间内,用binary search找到num应该在的位置去替换相应的数。这么做的目的是,例如上图中用5替换了7,假如5后面有6,7两个数的话,就会取得一个更长的subsequence。注意mid和num比较时使用的是>还是>=。这回区间头是移动到mid+1还是mid。
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
size = len(nums)
dp = []
for x in range(size):
if not dp or dp[-1] < nums[x]:
dp.append(nums[x])
low, high = 0, len(dp) - 1
while low < high:
mid = (low + high)/2
if dp[mid] < nums[x]:
low = mid + 1
else:
high = mid
dp[high] = nums[x] return len(dp)
Leetcode 300 Longest Increasing Subsequence的更多相关文章
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode] 300. Longest Increasing Subsequence (Medium)
题意: 求最长增长的子序列的长度. 思路: 利用DP存取以i作为最大点的子序列长度. Runtime: 20 ms, faster than 35.21% of C++ online submissi ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- LeetCode——300. Longest Increasing Subsequence
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- Mysql的二进制日志binlog的模式说明
binlog模式总共可分为以下三种:row,statement,mixed 1.Row日志中会记录成每一行数据被修改的形式,然后在slave端再对相同的数据进行修改,只记录要修改的数据,只有value ...
- Standard Error of Mean(s.e.m.)
· 来源:http://www.dxy.cn/bbs/thread/6492633#6492633 6楼: “据我所知,SD( standard deviation )反应的是观测值的变异性,其表示平 ...
- XMLHTTPRequest/Ajax请求 和普通请求的区别
Ajax请求头会多一个x-requested-with参数,值为XMLHttpRequest 详情:http://blog.csdn.net/zhangdaiscott/article/details ...
- MYSQL查询优化
目前手头有个查询: SELECT LPP.learning_project_pupilID, SL.serviceID, MAX(LPPO.start_date), SUM(LPPOT.license ...
- string to char* and char* to string 玩转 String 和 Char*
char 类型是c语言中常见的一个数据类型,string是c++中的一个,它的定义为 Strings are objects that represent sequences of character ...
- <实训|第十二天>用LVM对linux分区进行动态扩容
[root@localhost~]#序言在linux中,我们安装软件的途径一般有那些,你们知道吗?在linux中,如果你的磁盘空间不够用了,你知道如何来扩展磁盘吗?动态扩容不仅在工作中还是在其他方面都 ...
- .Net相关
Lucene 全文搜索 http://lucenenet.apache.org/ Memcached 分布式缓存 http://memcached.org/ selenium UI自动化测试 http ...
- nios II--实验5——定时器软件部分
软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...
- MPLS基础
1.1 MPLS简介 MPLS(Multiprotocol Label Switching,多协议标签交换)是一种新兴的IP骨干网技术.MPLS在无连接的IP网络上引入面向连接的标签交换概念,将第三 ...
- 【JQuery】 ajax 无效的JSON基元
[如题]个人理解就是 你向传数据[josn格式]了,但是后台接受确不是json格式的 数据, 贴段代码 var strJson = '{ "usercode": "123 ...