Longest Increasing Subsequence的两种算法
问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…<sn并且这个子序列的长度最长。简称LIS。
例如,对于数组[10, 9, 2, 5, 3, 7, 101, 18],它的最长递增子序列为[2,3,7,101],长度为4。当然最长递增子序列不止一个,例如[2,5,7,101],[2,5,7,18]都符合要求。
1.时间复杂度:O(n^2)
对于序列A(a1,a2,a3…..an),以序列中am元素为最小值的递增序列,只与am-1,am-2,……an有关,与am之前的元素无关,因此我们可以由后向前依次求出以每个元素为起始的最长递增序列。我们用一个数组flag存储最长递增子序列的长度,若i<j,A[i] < A[j],则flag[i] = max(flag[x], j <= x <= A.length) + 1。
1: public int lengthOfLIS(int[] nums) {
2: if (nums == null || nums.length == 0)
3: return 0;
4: int[] flag = new int[nums.length];
5: Arrays.fill(flag,1);
6: int maxLength = 1;
7: for (int i =nums.length - 2; i >= 0; i--){
8: int maxTemp = flag[i];
9: for (int j = i+1; j < nums.length;j++){
10: if (nums[i] < nums[j]){
11: maxTemp = maxTemp > flag[j] + 1 ? maxTemp : flag[j] + 1;
12: }
13: }
14: flag[i] = maxTemp;
15: maxLength = maxLength > flag[i] ? maxLength: flag[i];
16: }
17: return maxLength;
18: }
2.时间复杂度O(nlogN)
我们定义一个数组dp[],dp[i]表示长度为i的序列中最小的元素的值。例如对于序列A[10, 9, 2, 5, 3, 7, 101, 18],我们依次扫描每个元素,
首先对于A[0] = 10 , 我们可以得dp[1] = 10 ,表示序列长度为1的最小元素为10,此时序列为10,
对于A[1] = 9, 因为A[1] < dp[1],所以dp[1] = 9,此时序列为9
对于A[2] = 2, 因为A[2] < dp[1],所以dp[1] = 2,此时序列为2
对于A[3] = 5, 因为A[3] > dp[1],所以dp[2] = 5,此时序列为2,5
对于A[4] = 3, 因为A[4] < dp[2],所以dp[2] = 3,此时序列为2,3
对于A[5] = 7, 因为A[5] > dp[2],所以dp[3] = 7,此时序列为2,3,7
对于A[6] = 101, 因为A[6] > dp[3],所以dp[3] = 101,此时序列为2,3,7,101
对于A[7] = 18, 因为A[7] < dp[3],所以dp[3] = 18,此时序列为2,5,7,18
所以最长递增子序列的长度为4
1: int[] dp = new int[nums.length];
2: int len = 0;
3:
4: for(int x : nums) {
5: int i = Arrays.binarySearch(dp, 0, len, x);
6: if(i < 0) i = -(i + 1);
7: dp[i] = x;
8: if(i == len) len++;
9: }
10:
11: return len;
Longest Increasing Subsequence的两种算法的更多相关文章
- Longest Increasing Subsequence的两种解法
问题描述: 给出一个未排序队列nums,如[10, 9, 2, 5, 3, 7, 101, 18].找出其中最长的增长序列,但不是连续增长序列,如[2, 3, 7, 101]就是对应的最长增长序列LI ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 最长上升子序列 LIS(Longest Increasing Subsequence)
引出: 问题描述:给出一个序列a1,a2,a3,a4,a5,a6,a7….an,求它的一个子序列(设为s1,s2,…sn),使得这个子序列满足这样的性质,s1<s2<s3<…< ...
- Longest Increasing Subsequence - LeetCode
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 300最长上升子序列 · Longest Increasing Subsequence
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
随机推荐
- Module:template
ylbtech-Module: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech. ...
- Foreign Postcards
题意: 给定 n 张排成一堆的的卡片,每一次从堆顶上等概率随机取出 [1~当前卡片数] 个卡片,如果堆顶的卡片是反面朝上, 则将所有取出的卡片翻转,求问期望取出多少个反面朝上的卡片. 解法: 考虑dp ...
- HDFS源码分析四-HDFS Client
4. HDFS Client ( 未完待续 ) 目录: 4.1 认识 DFSClient ( 未完待续 ) 4.2 输入流 ( 未完待续 ) 4.3 输出流 ( 未完待续 ) 4.4 Distribu ...
- Apache CXF简介
Apache CXF是一个开源的,全功能的,容易使用的Web服务框架.CXF是由Celtix和XFire合并,在Apache软件基金会共同完成的.CXF的名字来源于"Celtix" ...
- C#backgroundWorker用法
1.在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停止响应.解决的方法就是新开一个线程,把耗时的操作放到线程中执行,这样就可以在用户界面上进行其它操作. ...
- SpringCloud之旅第一篇-微服务概念
一.单体架构的问题 微服务为什么会出现?在学习Springboot的时候知道Springboot极大的简化了我们的开发,我们可以快速的进行业务开发,Springboot单体应用在项目的开发初期能够满足 ...
- CodeForces 586D【BFS】
题意: s是这个人开始位置:连续相同大写字母是 Each of the k trains,相应的火车具有相应的字母: '.' 代表空: 有个人在最左列,上面有连续字母代表的火车,火车从左边出去的话,会 ...
- [Xcode 实际操作]三、视图控制器-(3)使用UINavigationController视图控制器
目录:[Swift]Xcode实际操作 本文将演示导航视图控制器的使用. 选择项目导航区的资源文件夹.需要导入两张图片,作为选项卡控制器的图标. [+]->[Import]->选择图片-& ...
- 用EnumSet代替位域
用EnumSet代替位域 如果一个枚举类型的元素主要用在集合中,一般使用int枚举模式,将2的不同倍数赋予每个常量: // Bit field enumeration constants - OB ...
- [UOJ22]外星人
题解 首先可以发现有效果的\(a_i\)大小一定是递减的,而且一定小于等于当前值 所以我们可以从大到小考虑每个\(a_i\),当确定了一个有效果的\(a_i\)时,\((a_i,x]\)的数都可以随意 ...