[LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS).
You code should return the length of the LIS.
Have you met this question in a real interview?
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4
Time complexity O(n^2) or O(nlogn)
What's the definition of longest increasing subsequence?
* The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
我们先来看一种类似Brute Force的方法,这种方法会找出所有的递增的子序列,并把它们都保存起来,最后再找出里面最长的那个,时间复杂度为O(n2),参见代码如下:
class Solution {
public:
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
int longestIncreasingSubsequence(vector<int> nums) {
vector<vector<int> > solutions;
longestIncreasingSubsequence(nums, solutions, );
int res = ;
for (auto &a : solutions) {
res = max(res, (int)a.size());
}
return res;
}
void longestIncreasingSubsequence(vector<int> &nums, vector<vector<int> > &solutions, int curIdx) {
if (curIdx >= nums.size() || curIdx < ) return;
int cur = nums[curIdx];
vector<int> best_solution;
for (int i = ; i < curIdx; ++i) {
if (nums[i] <= cur) {
best_solution = seqWithMaxLength(best_solution, solutions[i]);
}
}
vector<int> new_solution = best_solution;
new_solution.push_back(cur);
solutions.push_back(new_solution);
longestIncreasingSubsequence(nums, solutions, curIdx + );
}
vector<int> seqWithMaxLength(vector<int> &seq1, vector<int> &seq2) {
if (seq1.empty()) return seq2;
if (seq2.empty()) return seq1;
return seq1.size() < seq2.size() ? seq2 : seq1;
}
};
还有两种方法,(未完待续。。)
参考资料:
http://www.cnblogs.com/lishiblog/p/4190936.html
http://blog.xiaohuahua.org/2015/01/26/lintcode-longest-increasing-subsequence/
[LintCode] Longest Increasing Subsequence 最长递增子序列的更多相关文章
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
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 ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- 推荐15款最佳的 jQuery 分步引导插件
当用户浏览到一个网站,它可能从不知道如何浏览,如何操作网站或 Web 应用程序的内容和流程.在这篇文章中,我们编制了一些最好的 jQuery 引导插件列表.你会发现这些插件对于提高你的网站的整体用户体 ...
- Bootstrap之栅格系统
bootstrap 移动优先 中文官网 http://www.bootcss.com/ 1.基本模板 <!DOCTYPE html> <html lang="en&quo ...
- window对象的属性及事件。
不同的运行环境有不同的“顶层对象”,而在浏览器的环境中,顶层对象就是window对象.window就是指当前的浏览器窗口. 例:var a = 1: window.a; //1 1.window对象的 ...
- SharePoint 客户端对象模型共用ClientContext的坑
首先请看代码 private static void Main(string[] args) { Test2(); } private static void Test2() { var client ...
- 函数模块:CTVB_COMPARE_TABLES--两个表中删除/变更/粘贴分解
这个函数模块比较两个内表,将被删除.增加和修改的内表行分别分组输出. 输入参数:TABLE_OLD:旧表TABLE_NEW:新表KEY_LENGTH:键长度,指定内表中的前若干个字节(在 Unicod ...
- sharepoint 提升权限报错
现象: sharepoint中提升权限是为了模拟管理员操作,但是对于普通用户对item的更新和删除照成错误 解决办法: 去掉权限升级 说明: 升级权限能不用尽量不用,同时也不好排查权限问题.
- 浅谈RecyclerView(完美替代ListView,GridView)
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- Java避免创建不必要的对象
小Alan最近看到了<Effective Java>这本书,这本书包含的内容非常丰富,这本书我就不多介绍了,只能默默的说一句,作为一名java开发错过了这本书难免会成为一个小遗憾,所以还是 ...
- 分享dubbo.xsd和idubbo.xsd的可用地址
dubbo.xsd和idubbo.xsd的官方地址不可用 http://code.alibabatech.com/schema/dubbo/dubbo.xsd似乎挂了,真是淡淡的忧伤啊,然后就各种报错 ...
- C#winfrom播放器动态加载歌词
上周我们进行了结业项目答辩,是播放器项目.有一个关于播放器变唱歌边加载歌词的方法特别有意思,像酷狗那样子歌词和歌曲同步滚播的样子. 这里的工具是Visual Studio 2013,使用语言是C#和. ...