[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 ...
随机推荐
- Azure Storage Blob 属性设置
概述 在使用SDK做Blob对象属性的获取或设置时,如果只是直接使用get或set方法,是无法成功获取或设置blob对象的属性.主要是因为在获取对象时,对象的属性默认并未被填充到对象,这就需要执行额外 ...
- Fiddler设置代理抓手机包
启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 在Fiddler Options”对话框切换到“Connect ...
- 多线程-join()方法
在很多情况下,主进程创建并启动子线程,如果子线程中要进行大量的耗时运算,主线程往往将早于子线程结束之前结束.这时,如果主线程想等待子线程执行完成之后再结束,比如子线程处理一个数据,主线程要取得这个数据 ...
- 自定义闹钟 Reminder
Reminder reminder = ScheduledActionService.Find("MY REMINDER") as Reminder; if ( reminder ...
- Linux命令之rename
一.引言 今天才知道Linux下的rename有两个版本,util-linux工具集的rename和Perl版本的rename,而两者的用法是明显不一样的,Perl版rename相对比较强大 二.对比 ...
- CodeForces 584D Dima and Lisa
1e9 以内的判断一个数是否是素数,可以直接朴素的暴力. 这倒题除了考虑1e9以内的素数的判断,还有一个歌德巴赫猜想:任意一个奇数都可一分解为三个素数的和. 第三个结论:素数是密集的,1e9以内, ...
- ext,exrReturn新增,修改删除等用
package cn.edu.hbcf.common.vo; /** * Ext Ajax 返回对象 * * @author * @date 2012-02-21 19:30:00 * */ publ ...
- 1.2.1 Fragments - 碎片
在activity中,Fragment代表了一种行为和用户界面的一部分.在一个activity里,你可以联合多个fragment来创建一个多面板的UI,你也可以在多个activity里重复使用同一个f ...
- java.lang.IllegalArgumentException: n must be positive
public static String randomKey(){ Random random = new Random(); int key = random.nextInt(((int)Syste ...
- eclipse中根据方法找到其实现类
面向接口编程中,程序全是面向接口变成调用,在维护别人写的系统的时候怎么样快速定位当前根据接口调用的方法是哪个实体类实现的: Ctrl + T/f4(光标放在需要查看的方法上,然后按Ctrl+T或者F4 ...