[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 ...
随机推荐
- jQuery切换网页皮肤保存到Cookie实例
效果体验:http://keleyi.com/keleyi/phtml/jqtexiao/25.htm 以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//D ...
- html5 video
先简要概述一下video标签: video:嵌入视频到页面中 1. 声明video标签 单个视频的时候使用src: <video src="http://v2v.cc/~j/theor ...
- CSS float
我们来看看CSS重要属性--float. 以下内容分为如下小节: 1:float属性 2:float属性的特性 2.1:float之文字环绕效果 2.2:float之父元素高度塌陷 3:清除浮 ...
- MySQL外键与外键关系说明(简单易懂)
MySQL主键和外键使用及说明 一.外键约束 MySQL通过外键约束来保证表与表之间的数据的完整性和准确性. 外键的使用条件: 1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后 ...
- 关于SharePoint 2013的工作流(一)
从去年开始,一直和SharePoint 2013工作流打交道.自己瞎摸索,以实现功能为目的.直到如今也不知道走的路是否正确. 一开始用WF4发现整个都不一样了,用的xaml无法写后端代码.Google ...
- [DOM Event Learning] Section 4 事件分发和DOM事件流
[DOM Event Learning] Section 4 事件分发和DOM事件流 事件分发机制: event dispatch mechanism. 事件流(event flow)描述了事件对象在 ...
- iOS 学习 - 23 加载本地 txt 文件, NSMutableParagraphStyle 段落格式,缩放动画,字体间距
思路: 1.new 一个 Empty 后缀为 .txt 文件,内容随笔拷贝一段 2.用 NSString 接收本地文件,再用一个标题拼接字符串 3.创建一个 NSMutableParagraphSty ...
- rails中的form_for
1 form_for方法是ActionView::Helpers::FormHelper模块内的方法,所以可以在ActionView的实例中直接调用 2 from_for方法的原型为form_for( ...
- android handler ,message消息发送方式
1.Message msg = Message.obtain(mainHandler) msg.obj=obj;//添加你需要附加上去的内容 msg.what = what;//what消息处理的类 ...
- Redis 支持的5种数据结构
redis的崛起绝非偶然,它确实有自己的新东西在里面,它不像Memcached,只能将数据存储在内存中,它提供了持久化机制和数据同步,避免了宕机后的雪崩的问题,即服务器出现问题后,内存中保留的原始数据 ...