[Algorithms] Longest Increasing Subsequence
The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequence s, such that t satisfies two requirements:
- Elements in t are sorted in ascending order;
- t is as long as possible.
This problem can be solved using Dynamic Programming. We define the state P[i] to be the length of the longest increasing subsequence ends at i (with s[i] as its last element). Then the state equations are:
- P[i] = max_{j = 0, ..., i - 1 and arr[j] < arr[i]} P[j] + 1;
- If no such j exists, P[i] = 1.
Putting these into code using a table to store results for smaller problems and solve it in a bottom-up manner. We will have the following code.
#include <iostream>
#include <string>
#include <vector> using namespace std; int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
return maxlen;
} void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}
This program only computes the length of the LIS. If you want to print all the possible LIS, you need to modify the above program. Specifically, you may want to use backtracking to obtain all the possible LIS. My code is as follows. Welcome for any comments. Thank you!
#include <iostream>
#include <string>
#include <vector> using namespace std; /* Helper function to find all LCS. */
void findAllLCSHelper(vector<int>& nums, vector<int>& dp, vector<int>& seq, vector<vector<int> >& res, int maxlen, int end) {
if (maxlen == ) {
reverse(seq.begin(), seq.end());
res.push_back(seq);
reverse(seq.begin(), seq.end());
return;
}
for (int i = end; i >= ; i--) {
if (dp[i] == maxlen && (seq.empty() || nums[i] < seq.back())) {
seq.push_back(nums[i]);
findAllLCSHelper(nums, dp, seq, res, maxlen - , i - );
seq.pop_back();
}
}
} /* Function to find all LCS. */
vector<vector<int> > findAllLCS(vector<int>& nums, vector<int>& dp, int maxlen) {
vector<vector<int> > res;
vector<int> seq;
findAllLCSHelper(nums, dp, seq, res, maxlen, nums.size() - );
return res;
} /* Compute the length of LCS and print all of them. */
int longestIncreasingSubsequence(vector<int>& nums) {
vector<int> dp(nums.size(), );
int maxlen = ;
for (int i = ; i < (int)nums.size(); i++) {
for (int j = ; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + > dp[i]) {
dp[i] = dp[j] + ;
maxlen = max(maxlen, dp[i]);
}
}
}
vector<vector<int> > lcss = findAllLCS(nums, dp, maxlen);
for (int i = ; i < (int)lcss.size(); i++) {
for (int j = ; j < (int)lcss[i].size(); j++)
printf("%d ", lcss[i][j]);
printf("\n");
}
return maxlen;
} /* Test function. */
void longestIncreasingSubsequenceTest(void) {
int num[] = {, , , , , , , , , , , , , , , };
vector<int> nums(num, num + sizeof(num) / sizeof(int));
printf("%d\n", longestIncreasingSubsequence(nums));
} int main(void) {
longestIncreasingSubsequenceTest();
system("pause");
return ;
}
Running this program in Microsoft Visual Professional 2012 gives the following results.
The first four rows are the four LIS.
[Algorithms] Longest Increasing Subsequence的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [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 ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
随机推荐
- jboss部署web应用
http://liufei-fir.iteye.com/blog/759772初次部署jboss的web应用,把tomcat/weblogic下的工程移植到jboss上发布 一.修改JBOSS应用服务 ...
- Jboss下jaxws的开发
http://xpenxpen.iteye.com/blog/1695069之前用jaxws做web service开发,无论是axis2还是cxf的实现,在tomcat上均成功部署.偏偏项目用的是j ...
- c# 按位与,按位或
在工作中遇到按位或组合权限串.一直不是特别明白.今天终于花了半个下午的时间搞明白其中的道理. 首先每一个权限数都是2的N次方数 如:k1=2 ; //添加 k2=4 ; //删除 k3=8; //修改 ...
- Python sklearn 分类效果评估
https://blog.csdn.net/sinat_26917383/article/details/75199996
- php对二维数据进行排序
PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义. 方法一:(经验证,成功) 作用:对二维数组进行指定key排序 参数:$arr ...
- 并且需要用websocket实时接收数据 VS 组件ng2websocket的
chart.service.ts: import { Injectable } from '@angular/core'; import { WebSocketService } from './we ...
- OSGi规范概要
目前最新的OSGi规范是2012年7月发布的Release 5,Version5.0(后文简称为R5.0)版本,该规范定义了Java模块化系统所涉及的各种场景(开发.打包.部署.更新和交互等),以及其 ...
- iOS开发25个性能调优技巧
1. 用ARC管理内存 ARC(Automatic Reference Counting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为 ...
- C预编译宏
/* ============================================================================ Name : c_test001.c A ...
- sqlmap里如何添加字典
在sqlmap的目录下有那么一个目录.即"TXT"目录. 该目录下是放字典的. 我在日一个站的时候没有破解出表明.然后不小心下载到了数据库. sqlmap无法猜出表是啥.ps:ac ...