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?

长度为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最长递增子序列 动态规划)的更多相关文章

  1. LeetCode OJ:Longest Increasing Subsequence(最长递增序列)

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  2. Longest Increasing Subsequences(最长递增子序列)的两种DP实现

    一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn).     二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...

  3. 65.Longest Increasing Subsequence(最长增长子序列)

    Level:   Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...

  4. POJ2533 Longest Ordered Subsequence 【最长递增子序列】

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Acc ...

  5. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  6. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  7. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  8. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  9. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

随机推荐

  1. WinError 5

    IDE工具:pychrm 语言:python 在使用os模块修改路径名称时,总是会报 WinError 5 这个错误,就是拒绝访问,之前也遇见过,就是要操作的当前路径里有文件已经打开,代码不能再次访问 ...

  2. c# word excel 二进制 存入数据库

    在Sql Server中存储.读写Word文件,需要将指定表字段添加为Image类型,示例表结构为:1 CREATE TABLE CONTRACTS ( 2 ID VARCHAR (50), 3 CO ...

  3. apacheserver全局配置具体解释

    server标识相关指令: ServerName ServerAdmin ServerSignature ServerTokens UseCanonicalName UseCanonicalPhysi ...

  4. -pie can only be used when targeting iOS 4.2 or later

    网上下载的demo,执行报错-pie can only be used when targeting iOS 4.2 or later 解决的方法:选择target==>改动developmen ...

  5. 模拟window桌面实现

    正在开发中的游戏有个全屏功能--可以在window桌面背景上运行,就像一些视频播放器在桌面背景上播放一样的,花了个上午整了个Demo放出来留个纪念. 实现功能:显示图标,双击图标执行相应的程序,右击图 ...

  6. erase操作

    #include<iostream> #include <vector> int main() { std::vector<int> vec; vec.push_b ...

  7. nginx简单的nginx.conf配置

    nginx.conf配置如下: #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log ...

  8. Servlet------>servletDemo 及细节注意

    原理图: 前提:我用的命令行都是mac系统下用的,非win jsp实质是一个servlet,所以要先了解servlet,如上页面是一个servletdemo,下面是尝试的步骤 1.先写好Demo.ja ...

  9. Struts2+Spring3+MyBatis3整合以及Spring注解开发

     分类: Web(2)  版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做一个SpringMVC+spring+MyBatis的项目,突然想起以前自己要搭建一个Struts2+Sprin ...

  10. shell awk实战

    一.文本处理 1.按行提取关键字频次(如取第5列) awk 'BEGIN{FS="|"} {a[$5]+=1;} END {for(i in a) print i ":& ...