300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
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?
长度为N的数组记为A={a0 a1 a2...an-1};
记A的前i个字符构成的前缀串为Ai= a0 a 1a2...ai-1,以ai结尾的最长递增
子序列记做Li,其长度记为a[i];
假定已经计算得到了a[0,1…,i-1],如何计算a[i]呢?
根据定义, Li必须以ai结尾,如果将ai缀到L0 L1…… Li-1的后面,是否
允许呢?
如果aj<ai,则可以将ai缀到Lj的后面,并且使得Lj的长度变长。
从而:a[i]={max(a(j))+1, 0 ≤j≤i-1且a[j]≤a[i] }
需要遍历在i之前的所有位置j,找出满足条件a[j]≤a[i]的a[j];
计算得到a[0…n-1]后,遍历所有的a[i],找出最大值即为最大递增子序列
的长度。
时间复杂度为O(N2)。
思考:如何求最大递增子序列本身?
记录前驱
class Solution {
public int lengthOfLIS(int[] a) {
if(a.length==0) return 0;
int[] longs = new int[a.length];
for(int i = 0;i<a.length;i++)
longs[i] = 1;
int max = longs[0];
for(int i = 1;i < a.length;i++){
for(int j = 0;j <= i-1;j++)
if(a[j]<a[i])
if(longs[i]<longs[j]+1)
longs[i] = longs[j]+1;
//如果求序列本身,在这里记录前驱
if(max<longs[i])
max = longs[i];
}
return max;
}
class Solution {
public:
int lengthOfLIS(vector<int>& a) {
if (a.size()==) return ;
int max = ;
vector<int> dp(a.size(),);
for (int i = ;i < a.size(); i++) {
for (int j = ; j < i ;j++) {
if (a[j] < a[i]) {
dp[i] = std::max(dp[j]+,dp[i]);
}
}
max = std::max(dp[i],max);
}
return max;
}
};
class Solution(object):
def lengthOfLIS(self, a):
n = len(a)
if n ==0:
return 0
dp = [1] * n
for i in range(n):
for j in range(i):
if(a[i]>a[j] and dp[i]<dp[j]+1):
dp[i] = dp[j]+1 #dp[i]现在存储的即为以a[i]结尾最长递增子序列长度
#求dp数组中最大者即为最长的长度 return max(dp)
300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)的更多相关文章
- LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- 65.Longest Increasing Subsequence(最长增长子序列)
Level: Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...
- POJ2533 Longest Ordered Subsequence 【最长递增子序列】
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32192 Acc ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- [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最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
随机推荐
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- mysql解决乱码问题
进入mysql(mysql -u root -p),查看当前数据库字符集(status;) 刚开始是latin1,所以乱码. vim /etc/my.cnf 两个节点添加如下: [client]def ...
- VS编译错误:fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this problem
fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this ...
- 使用js里面的迭代器filter实现数组去重
实现数组去重的方法很多,最原始的方法是一个值一个值的去遍历,写到空数组里面: let r=[],arr = ['a', 'b', 'c', 'a']; for(var i=0,len=arr.leng ...
- 写一个SingleTon,(饿最终、懒同步)
1.饿汉式: public class SingleTon { private SingleTon(){ } private final static SingleTon instance = new ...
- MySQL的system命令在渗透测试中的使用以及UDF提权
声明:下面引用关于SYSTEM的东西是自己之前的内容,是自己没有研究透导致的错误结论:有了错就要改,做技术的不能弄虚作假,时时刻刻要求自己要谨慎,虽然我的博客没人看,但是也要向所有已经看到的人道歉,错 ...
- [Jenkins] Manage Jenkins from Web Interface
URL 说明 [jenkins_url]/safeRestart This will restart Jenkins after the current builds have completed. ...
- Openstack实现共有云多flat网络
首先给两台虚拟机添加网卡,模式为仅主机模式 配置控制节点IP /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=sta ...
- R语言中获取当前目录
# 获取当前工作目录 getwd() # 设置工作目录 setwd()
- jsp+servlet+mvc模式图
在我们的开发中,最常用的开发模式莫过于MVC模式,即M--MODEL.V--View.C--Controller,这样不仅可以方便开发人员分工协作,提高开发效率,增强程序的可维护性和拓展性,而且利用C ...