Longest Increasing Subsequence 最长递增子序列

子序列不是数组中连续的数。

dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列。

初始化是dp所有的都为1,最终的结果是求dp所有的数值的最大值。

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int length = nums.size();
if(length <= )
return ;
vector<int> dp(length,);
int max_num;
for(int i = ;i < length;i++){
max_num = ;
for(int j = i - ;j >= ;j--){
if(nums[i] > nums[j])
max_num = max(max_num,dp[j] + );
}
dp[i] = max_num;
}
max_num = ;
for(int i = ;i < length;i++){
if(dp[i] > max_num)
max_num = dp[i];
}
return max_num;
}
};

674. Longest Continuous Increasing Subsequence

相对于最长递增子序列来说,这个题目更加简单,只需要比较前一个数字就好,不用把前面的数字都比较完。因为如果比前一个小,直接就无法完成递增,只能保持当前的值1;如果比前一个数字大,其实前一个数字就已经计算了之前递增的个数,直接加1就好了

class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if(nums.empty())
return ;
int length = nums.size();
vector<int> dp(length,);
for(int i = ;i < length;i++){
if(nums[i] > nums[i-])
dp[i] = dp[i-] + ;
}
int max_num = ;
for(int i = ;i < length;i++)
max_num = max(max_num,dp[i]);
return max_num;
}
};

leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

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

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

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

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

  8. 最长递增子序列 (LIS) Longest Increasing Subsequence

    问题描述: 有一个长为n的数列a0, a1,..., an-1.请求出这个序列中最长的上升子序列.请求出这个序列中最长的上升子序列. 上升子序列:对于任意i<j都满足ai<aj的子序列. ...

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

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

随机推荐

  1. HDOJ2037 今年暑假不AC (经典的贪心问题)

    Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会 ...

  2. 自定义scoll样式

    使用伪类自定义scroll样式 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  3. LOJ572: Misaka Network 与求和

    传送门 假设 \(f^k(i)\) 就是 \(f(i)\) 莫比乌斯反演得到 \[ans=\sum_{i=1}^{N}\lfloor\frac{N}{i}\rfloor^2\sum_{d|i}f(d) ...

  4. 关于MyEclipse2017Ci10版本的破解和Tomcat9.0的安装搭配使用

    昨天和今天就忙这两件事情了.废话不多说直接上干货! 首先是关于Myeclipse2017的破解,关于这个破解,网上的资源和文件很多,可以自行下载,我就不贴链接了. 我要说的是破解的问题,在这里我们要注 ...

  5. drupal7 转化 public:// 为实际url

    file_create_url('public://xxx.png'); // 得到URL drupal_realpath('public://xxx.png'); // 得到系统路径(磁盘路径,如D ...

  6. SQL-字符串运算符和函数

    COALESCE(columnname,string) 函数 将 NULL 值作为字符串(用空字符串或其他字符串替换 NULL)- 接受一列输入值(字段)如果该字段为 NULL,则返回后面替换的字符串 ...

  7. D3.js 入门教程

    最近需要用到d3, 记录下d3的教程 网上搜了几个关于d3的教程 D3.js 入门教程      http://wiki.jikexueyuan.com/project/d3wiki/author.h ...

  8. Gensim入门教程

    What is Gensim? Gensim是一款开源的第三方Python工具包,用于从原始的非结构化的文本中,无监督地学习到文本隐层的主题向量表达.它支持包括TF-IDF,LSA,LDA,和word ...

  9. 何时需要做urlEncode,以及为什么要做

    在RFC1738中,对于URL可以使用的字符集做了如下规定: “ 只有0-9a-zA-Z的字母以及$-_.+!*'(),"这几个特殊字符 ” 而在html4中扩展了所有的unicode ch ...

  10. NexusFile(文件管理器)

    NexusFile是一款来自于韩国的文件管理器,类似于TotalCommander,拥有两个并排的文件夹窗口. NexusFile软件的特性如下: 基本功能:复制/移动, 复制/剪切/粘贴, 删除/擦 ...