[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 ...
随机推荐
- 深入理解和应用Float属性
一.Float的特性 1. 应用于文字围绕图片 2. 创建一个块级框 3. 多列浮动布局 4. 浮动元素的宽度.高度自适应,但可以设置其值. 二.核心解决的问题 文字围绕图片:img标签与多个文本标签 ...
- JavaScript调试 - debugger语句
语法: debugger 作用: 启动调试器 备注: 1. 可以将debugger语句放在过程的任何地方以中止执行.2. 使用debugger语句类似于在代码中设置断点. 3. debugger语句中 ...
- 游标的使用——mysql
CREATE DEFINER=`root`@`%` PROCEDURE `split_category_all`()BEGIN declare categ varchar(10); ##套餐列 dec ...
- AlloyRenderingEngine入门
写在前面 AlloyRenderingEngine是一款非常快速的渲染引擎,目前该项目已经合并至 https://github.com/AlloyTeam/AlloyGameEngine/ , 属于A ...
- AMD and CMD are dead之KMDjs在JS工程化的努力
总览 kmdjs发布了最接近最终版本的0.0.4版本https://github.com/kmdjs/kmdjs,你已经完全可以在项目中使用.我已经无法用语言形容其完美程度.借用我发的微博: 模块 ...
- 实现UITextView的placeholder
我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...
- 【转】Xcode进阶快捷键
Xcode 快捷键和手势不仅节省了宝贵的工作时间,而且能让你在工作过程中感到更自信.能力变得更强,这样的工作方式也更合理.学习下列技巧你将成为 Xcode 资深用户. 此处提供一些通用的按键符以供参考 ...
- GCD定时器
// // ViewController.m // GCD 定时器 // // #import "ViewController.h" NSInteger count = ; @in ...
- 浅谈UIAlertController使用
一开始在刚接触到Alert和ActionSheet的时候,经常傻傻分不清楚,好不容易用习惯了,苹果又给合并了,好在用起来也不困难,到底哪个好呢?见仁见智吧! 现在稍微介绍一下怎么用. 1.初始化,一般 ...
- CocoaPods 导入第三方库头文件自动补齐
使用了一段时间CocoaPods来管理Objective-c的类库,方便了不少.但是有一个小问题,当我在xcode输入import关键字的时候,没有自动联想补齐代码的功能,需要手工敲全了文件名,难以适 ...