问题定义:

给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续)。

这道题共3种解法。

1. 动态规划

动态规划的核心是状态的定义和状态转移方程。定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度。可以得到以下的状态转移方程:

d(i) = max(, d(j) + ), 其中j < i,且A[j] <= A[i]

程序实现:

int longestIncreasingSubsequence(vector<int> nums)
{
if (nums.empty())
return ;
int len = nums.size();
vector<int> lis(len, ); for (int i = ; i < len; ++i)
{
for (int j = ; j < i; ++j)
{
if (nums[j] < nums[i] && lis[i] < lis[j] + )
lis[i] = lis[j] + ;
}
}
return *max_element(lis.begin(), lis.end());
}

程序复杂度为O(N^2)

2. 动态规划 + 二分查找

换一种角度看问题。令Ai,j表示所有长度为j的最大递增子序列的最小末尾,我们有Ai,1 < Ai,2 < ... < Ai,j。

对A[i+1]来说,有两种选择。

1. Ai,j < A[i+1], 此时我们可以得到一个长度为i+1的最大递增子序列。

2. 替换Ai,k,如果Ai,k-1 < A[i+1] < Ai,k。

替换长度为k的最大递增子序列的最小末尾,是为了增大获取更长子序列的机会。

程序实现:

int binarySearch(const int arr[], int low, int high, int val)
{
while (low <= high)
{
int mid = low + (high - low) / ; // Do not use (low + high) / 2 which might encounter overflow issue if (val < arr[mid])
high = mid - ;
else if (val > arr[mid])
low = mid + ;
else
return mid;
}
return low;
}
int LIS(int arr[], int n)
{
int *minTail = new int[n];
minTail[] = arr[];
int len = ;
for (int i = ; i < n; ++i)
{
if (arr[i] > minTail[len-])
minTail[len++] = arr[i];
else
{
int pos = binarySearch(minTail, , len-, arr[i]);
minTail[pos] = arr[i];
}
}
delete [] minTail;
return len;
}

复杂度:O(nlogn)

reference :

最长递增子序列(LIS)

3. 排序+LCS

这种方法就不细说了。。。

最长递增子序列(Longest increasing subsequence)的更多相关文章

  1. 【转】动态规划:最长递增子序列Longest Increasing Subsequence

    转自:https://www.cnblogs.com/coffy/p/5878915.html 设f(i)表示L中以ai为末元素的最长递增子序列的长度.则有如下的递推方程: 这个递推方程的意思是,在求 ...

  2. 算法实践--最长递增子序列(Longest Increasing Subsquence)

    什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...

  3. 300最长上升子序列 · Longest Increasing Subsequence

    [抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...

  4. [Swift]LeetCode300. 最长上升子序列 | Longest Increasing Subsequence

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

  5. 最长递增子序列(Longest Increase Subsequence)

    问题 给定一个长度为N的数组,找出一个最长的单调自增子序列(不一定连续,但是顺序不能乱).例如:给定一个长度为6的数组A{5, 6, 7, 1, 2, 8},则其最长的单调递增子序列为{5,6,7,8 ...

  6. nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)

    最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n).  具体分析参考:http://b ...

  7. 动态规划--最长上升子序列(Longest increasing subsequence)

    前面写了最长公共子序列的问题.然后再加上自身对动态规划的理解,真到简单的DP问题很快就解决了.其实只要理解了动态规划的本质,那么再有针对性的去做这方的题目,思路很快就会有了.不错不错~加油 题目描述: ...

  8. [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

    Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...

  9. [Swift]LeetCode594. 最长和谐子序列 | Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

随机推荐

  1. React Native简史

    诞生 React Native 诞生于 2013 年的 Facebook 内部黑客马拉松(hackathon): In the essence of Facebook’s hacker culture ...

  2. sprigboot 异常 Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].Tomc...

    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...

  3. 分享一本Java并发编程的免费好书

    最近当当的大促销又开始了,估计很多人脑子一热,又花钱囤了不少技术书吧. 在我看来大部分程序员买技术书的用途(以下排名按用途从大到小): 让领导.同事看见,你看我多爱学习: 给自己一个心理安慰,我还没废 ...

  4. php.ini配置文件详解(基于5.2.17版本)

    [PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; 关于php.ini文件 ;;;;; ...

  5. CKEDITOR (FCKEDITOR) --- 目前最优秀的可见即可得网页编辑器之一

    FCKEDITOR 编辑 同义词 CKEditor一般指FCKEDITOR FCKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写.具备功能强大.配置容易.跨浏览器. ...

  6. 【three.js第二课】页面自适应

    1.在[three.js第一课]的基础上加入以下代码,改变窗口大小时,页面内容会自适应 //加入事件监听器,窗口自适应 window.addEventListener('resize', functi ...

  7. python2.7安装pip

  8. 【MyBatis深入剖析】应用分析与最佳实践

    ##### 文章目标1. 了解ORM框架的发展历史,了解MyBatis特性2. 掌握MyBatis编程式开发方法和核心对象3. 掌握MyBatis核心配置含义4. 掌握MyBatis的高级用法与扩展方 ...

  9. E - Dividing Chocolate ATcoder

    题目大意:切割图形,给你一个非0即1的矩阵,将它切割成多个长方形,使每个小长方形中1的个数不得多于k个,切割的规则,要么切一整行,要么是一整列. 题解: 二进制枚举. 注意行数最大才是10.用二进制枚 ...

  10. C - Dr. Evil Underscores CodeForces - 1285D 二进制

    题目大意:n个数,任意整数x对这n个数取异或值,然后使最大值最小. 思路:数据范围最大为pow(2,30);所以考虑二进制的话,最多有30位.对于某一位d,然后考虑数组v中每一个元素的d为是0还是1, ...