题目:

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. Django之中间件-CSRF

    CSRF a.CSRF原理 post提交时需要提交csrf_token ,缺少则不通过 在form表单中加入: {% csrf_token %} b.无CSRF时存在隐患 防护其他人通过别的链接pos ...

  2. Python中strip方法的妙用

    [开胃小菜] 当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格.有下面两种方法来实现. 方法一:用内置函数 #<python> if __name ...

  3. cacti 主机/网络设备流量监控 图解

    1.在配置中找到设备 console —>  Device 2.初次添加 cacti 监控主机的时候是没有任何设备的,所以要选择add 添加你要监控的主机 \

  4. Nginx 经验小结

    chmod 777 永远不要 使用 777,有时候可以懒惰的解决权限问题, 但是它同样也表示你没有线索去解决权限问题,你只是在碰运气. 你应该检查整个路径的权限,并思考发生了什么事情. 把 root ...

  5. [转]Win10输入法图标消失且只能输入英文的解决方法

    今天电脑开机后发现输入法图标不见了,而且只能输入英文,上网查了很多资料终于找到了解决方案,现摘录如下,以防再次遇到问题,便于查找.谢谢提供解决方案的大牛,如有侵权,请联系本人进行删除(文末放置了原文地 ...

  6. mac os x升级MOUNTAIN LION后svn command not found的解决

    因为svn是个开发工具 所以苹果把他移到 Xcode developer package 里 去了,所以你没装xcode之类的,先去AppStore把xcode装了   装好之后sudo vi /et ...

  7. 消息队列Handler的用法

    下面是每隔一段时间就执行某个操作,直到关闭定时操作: final Handler handler = new Handler(); Runnable runnable = new Runnable() ...

  8. 14、AppWidget及Launcher RemoteViews

    一.Launcher的简单研究 1 什么是Launcher Android系统启动后加载的第一个程序 . 这个程序是其他应用程序的入口 . Launcher构成: HomeScreen : (Work ...

  9. python 基础 4.2 高阶函数上

    一.高阶函数 把函数当做参数传递的一种函数   1>map()函数 map函数是python内置的一个高阶函数,它接受一个函数f和一个list,并把list元素以此传递给函数f,然后返回一个函数 ...

  10. 【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法

    [BZOJ1336][Balkan2002]Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=10000 ...