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: ...
随机推荐
- H&M
H&M于1947年由Erling Persson在瑞典创立.如今,H&M在全世界1500 多个专卖店销售服装.配饰与化妆品.位于瑞典市Stora Gatan大街的老H&M店是世 ...
- LED音乐频谱之概述
点击打开链接 转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/37929733 这个LED音乐频谱是我在学51单片机的 ...
- 插件—jquery.validate.js
前言 在学习jquery.validate.js中的一个小案例,只是这个插件的简单使用. 案例代码如下: <head> <title></title> ...
- 【BZOJ1050】[HAOI2006]旅行comf 并查集
[BZOJ1050][HAOI2006]旅行comf Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<300 ...
- UVA12470—Tribonacci (类似斐波那契,简单题)
题目链接:https://vjudge.net/problem/UVA-12470 题目意思:我们都知道斐波那契数列F[i]=F[i-1]+F[i-2],现在我们要算这样的一个式子T[i]=T[i-1 ...
- JQueryiframe页面操作父页面中的元素与方法(实例讲解)
1)在iframe中查找父页面元素的方法:$('#id', window.parent.document) 2)在iframe中调用父页面中定义的方法和变量:parent.methodparent.v ...
- Nagle's algorithm
w41字节的数据包只有1字节的可用信息.以减少数据包发送量来提高TCP/IP网络性能. https://en.wikipedia.org/wiki/Nagle's_algorithm https:// ...
- d3.js 之SVG:矢量化图形绘制
SVG Scalable Vector Graphics 是一个成熟的W3C标准,被设计用来在web和移动平台 上展示可交互的图形.和HTML类似,SVG也支持CSS和JavaScript.尽管可以使 ...
- String Problem --- hdu3374(kmp、字典序最大与最小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意很简单,输出的是最小字典序的编号,最小字典序个数,最大字典序编号,最大字典序个数. 可以想一 ...
- restfull规范
web服务交互 我们在浏览器中能看到的每个网站,都是一个web服务.那么我们在提供每个web服务的时候, 都需要前后端交互,前后端交互就一定有一些实现方案,我们通常叫web服务交互方案. 目前主流的三 ...