【Lintcode】076.Longest Increasing Subsequence
题目:
Given a sequence of integers, find the longest increasing subsequence (LIS).
You code should return the length of the LIS.
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_increasing_subsequence
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [2, 4, 5, 7], return 4
题解:
For dp[i], dp[i] is max(dp[j]+1, dp[i]), for all j < i and nums[j] < nums[i].
Solution 1 ()
class Solution {
public:
int longestIncreasingSubsequence(vector<int> nums) {
if (nums.empty()) {
return ;
}
vector<int> dp(nums.size(), );
int res = ;
for (int i = ; i < nums.size(); ++i) {
for (int j = ; j < i; ++j) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + );
}
}
res = max(dp[i], res);
}
return res;
}
};
Solution 2 ()
class Solution {
public:
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
int longestIncreasingSubsequence(vector<int> nums) {
vector<int> res;
for(int i=; i<nums.size(); i++) {
auto it = std::lower_bound(res.begin(), res.end(), nums[i]);
if(it==res.end()) res.push_back(nums[i]);
else *it = nums[i];
}
return res.size();
}
};
Solution 3 ()
class Solution {
public:
int longestIncreasingSubsequence(vector<int> nums) {
if (nums.empty()) {
return ;
}
vector<int> tmp;
tmp.push_back(nums[]);
for (auto num : nums) {
if (num < tmp[]) {
tmp[] = num;
} else if (num > tmp.back()) {
tmp.push_back(num);
} else {
int begin = , end = tmp.size();
while (begin < end) {
int mid = begin + (end - begin) / ;
if (tmp[mid] < num) {
begin = mid + ;
} else {
end = mid;
}
}
tmp[end] = num;
}
}
return tmp.size();
}
};
【Lintcode】076.Longest Increasing Subsequence的更多相关文章
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Lintcode】077.Longest Common Subsequence
题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
- 【题解】Greatest Common Increasing Subsequence
[题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【刷题-LeetCode】300. Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- 【Leetcode_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
- 【leetcode】521. Longest Uncommon Subsequence I
problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...
- 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...
随机推荐
- centos部署Python环境
在centos上部署Python之前,我们需要先配置开发环境. 1.安装Python依赖的开发工具包 gcc自然少不了,可以直接用“Development Tools”: yum grouplist ...
- 提高网站打开速度的7大秘籍---依据Yslow工具的优化【转】
很多站长使用虚拟主机来做网站,网页内容一旦很多,网站打开速度就会特别慢,如果说服务器.带宽.CDN这类硬指标我们没有经济实力去做,不妨通过网页代码优化的方式来提高速度,卢松松总结了一些可行性的方法. ...
- intellij 开发webservice
最近项目中有用到WebService,于是就研究了一下,但是关于intellij 开发 WebService 的文章极少,要不就是多年以前,于是研究一下,写这篇博文.纯属记录,分享,中间有不对的地方, ...
- C语言高速入门系列(四)
C语言高速入门系列(四) C语言数组 ---------转载请注明出处:coder-pig 贴心小提示:假设图看不清晰可右键另存为,应该就非常清晰了; 注意上面的代码都要自己过一遍哦! 本节引言: 经 ...
- 【selenium+Python unittest】之使用smtplib发送邮件错误:smtplib.SMTPDataError:(554)、smtplib.SMTPAuthenticationError(例:126邮箱)
原代码如下: import smtplib from email.mime.text import MIMEText from email.header import Header #要发送的服务器 ...
- python 基础 8.1 r 正则对象
...
- python 基础 7.2 时间格式的相互转换
#/usr/bin/python #coding=utf-8 #@Time :2017/11/9 8:55 #@Auther :liuzhenchuan #@File :时间格式的相互转换.p ...
- bootstrap中模态框的大小设置;
bootstrap模态框调节大小: 大尺寸:黑体加大的字体,是更改的代码 <!-- 大模态框的调节 --> <button type="button" class ...
- 五个知识体系之-SQL学习-第一天
1. 创建数据库 CREATE DATABASE test1; 2. 删除数据库 DROP DATABASE test1; 3. 创建表 CREATE TABLE tabname (userid BI ...
- MongoDB C#驱动:
MongoDB C#驱动: http://xiaosheng.me/2016/09/15/article24 http://www.cnblogs.com/wuhuacong/p/5098348.ht ...