给出一个无序的整形数组,找到最长上升子序列的长度。
例如,
给出 [10, 9, 2, 5, 3, 7, 101, 18],
最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4。因为可能会有超过一种的最长上升子序列的组合,因此你只需要输出对应的长度即可。
你的算法的时间复杂度应该在 O(n2) 之内。
进阶: 你能将算法的时间复杂度降低到 O(n log n) 吗?

详见:https://leetcode.com/problems/longest-increasing-subsequence/description/

Java实现:

class Solution {
public int lengthOfLIS(int[] nums) {
int n=nums.length;
if(n==0||nums==null){
return 0;
}
int[] dp=new int[n];
Arrays.fill(dp,1);
int res=1;
for(int i=1;i<n;++i){
for(int j=0;j<i;++j){
if(nums[j]<nums[i]&&dp[j]+1>dp[i]){
dp[i]=dp[j]+1;
}
if(res<dp[i]){
res=dp[i];
}
}
}
return res;
}
}

C++实现:

方法一:

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> maxLen(size,1);
int res=1;
for(int i=1;i<size;++i)
{
for(int j=0;j<i;++j)
{
if(nums[j]<nums[i]&&maxLen[j]+1>maxLen[i])
{
maxLen[i]=maxLen[j]+1;
}
if(res<maxLen[i])
{
res=maxLen[i];
}
}
}
return res;
}
};

方法二:

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int size=nums.size();
if(size==0||nums.empty())
{
return 0;
}
vector<int> res;
for(int i=0;i<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();
}
};

参考:https://www.cnblogs.com/grandyang/p/4938187.html

300 Longest Increasing Subsequence 最长上升子序列的更多相关文章

  1. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  2. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  3. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  4. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  5. [LeetCode] Longest Increasing Subsequence 最长递增子序列

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

  6. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  7. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  8. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  9. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

随机推荐

  1. hdu - 2266 How Many Equations Can You Find (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可 ...

  2. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

  3. POJ 2411_Mondriaan's Dream

    题意: 用1*2和2*1的方块将给定长宽的矩形填满.问有多少种放法,对称的算两种. 分析: 状态压缩dp 首先用0表示前一行没有竖块占用这个位置,而1表示该位置和他上方的位置放了一个竖块,从而压缩状态 ...

  4. [bzoj4520][Cqoi2016]K远点对_KD-Tree_堆

    K远点对 bzoj-4520 Cqoi-2016 题目大意:已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. 注释:$1\le n\le 10^5$,$1\le k\le 100$,$k\l ...

  5. linux下crontab安装和使用(定时任务)

    在Unix和Linux的操作系统之中,cron可以让系统在指定的时间,去执行某个指定的任务,crontab命令常用于管理对应的cron. 一.crontab在线安装 yum -y install vi ...

  6. Oracle SqlPlus导出查询结果

    Oracle SqlPlus导出查询结果 在sqlplus下导出查询的结果保存到本地sql文件中,可以采用如下方式:1.连接数据库: sqlplus xmq/xmqpwd@192.168.1.57:1 ...

  7. 《Java设计模式》之状态模式

    状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模式. 状态模式同意一个对象在其内部状态改变的时候改变其行为.这个对象看上去就像是改变了它 ...

  8. C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释

    RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...

  9. JavaScript变量提升演示样例

    直接先看两段代码 function getSum() { var sum = a + b; var a = 1; var b = 2; return sum; } getSum(); function ...

  10. Java:String和Date、Timestamp之间的转换【转】

    原文地址:http://yunnick.iteye.com/blog/1074495 一.String与Date(java.util.Date)互转 1.1 String -> Date Str ...