题目:

Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Clarification

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

Example

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的更多相关文章

  1. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  2. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【Lintcode】077.Longest Common Subsequence

    题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...

  4. 【题解】Greatest Common Increasing Subsequence

    [题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...

  5. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  6. 【刷题-LeetCode】300. Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  7. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

  8. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

  9. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

随机推荐

  1. Multicast注册中心

    1 2 3 4 提供方启动时广播自己的地址. 消费方启动时广播订阅请求. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者. 消费方收到提供方地址时, ...

  2. markdownPad2 绿色破解版安装

    markdownpad2,默认为markdown传统风格,不能编辑分行的代码段,可以在设置里改为Github 风格,但需要付费激活,以下是绿色安装包,附激活方法 下载地址 https://pan.ba ...

  3. Svn服务器备份迁移小结

    注:svn备份千万不要采用打包压缩,然后解压文件的方式. 备份和还原之前先要关掉svn服务器. svn备份一般采用三种方式: 1)svnadmin dump 2)svnadmin hotcopy 3) ...

  4. 多媒体开发之---h264中 的RTP PAYLOAD 格式

    H.264 视频 RTP 负载格式 1. 网络抽象层单元类型 (NALU) NALU 头由一个字节组成, 它的语法如下: +---------------+      |0|1|2|3|4|5|6|7 ...

  5. JS基础知识再整理..........不断更新中

    1.JS的五种基本数据类型:字符串.数值.布尔.null.underfined. 2.在JS中,字符串.数值.布尔三种数据类型,有其属性和方法: 3.字符串的三种常用方法[.indexof()..su ...

  6. Linq Group By 多个字段

    var counts = dal.QueryStatisticsCount(condition); var result = from p in counts group p by new { Auc ...

  7. SQL 经验总结

    总结日常工作中使用SQL遇到的坑和技巧,一是加深印象,二是方便回顾.会不定期更新. 坑 1.多表联查时要使用表名,如果两个表的有列名相同的情况你没有加别名,那么sql编译器就不知道使用哪个列.这时进行 ...

  8. stacked generalization 堆积正则化 堆积泛化 加权特征线性堆积

    https://en.wikipedia.org/wiki/Ensemble_learning Stacking Stacking (sometimes called stacked generali ...

  9. mysql系列之8.mysql高可用 (mha4mysql)

    环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...

  10. 【题解】P4886快递员

    [题解]P4886 快递员 淀粉质好题!!!加深了我对点分治的理解.最近分治学了好多啊. 题目大意 给定你一颗有边权的树,再给你\(m\)和点对,请你在树上选出来一个点,使得所有点对到这个点的距离的最 ...