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. [linux] C语言Linux系统编程进程基本概念

    1.如果说文件是unix系统最重要的抽象概念,那么进程仅次于文件.进程是执行中的目标代码:活动的.生存的.运行的程序. 除了目标代码进程还包含数据.资源.状态以及虚拟化的计算机. 2.进程体系: 每一 ...

  2. 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架

    转自:https://blog.csdn.net/eson_15/article/details/51312490 前面两节,我们整合了SSH并且抽取了service和action部分的接口,可以说基 ...

  3. stringstream快速实现String和int之间的转换

    需要包含头文件”sstream” #include <iostream> #include <string> #include <sstream> using na ...

  4. Spring入门(二)— IOC注解、Spring测试、AOP入门

    一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...

  5. 解决ImmediateDeprecationError 用Python获取Yahoo数据

    最近正在看用 python 进行数据处理的内容,很多教程都会用 pandas 去抓取金融数据.我也尝试跑教程上的示例代码以抓取数据. 本文着重介绍遇到的问题以及解决方法. 注:我使用的是 Python ...

  6. C# 进程通信-命名管道

    之前看wcf服务的时候看到wcf有支持管道通信协议,之前不知道,最近刚好有用到这个,这里写个简单实例 .net有已经封装好的pip通信的对象NamedPipeServerStream 和NamedPi ...

  7. 【一些简单的jQuery选择器】

    学习[js DOM 编程艺术],最后面有许多jQuery的选择器,每个都动手敲了一遍. jQuery 提供了高级选择器的方法. js获取元素的三个基本方法分别是通过标签名,类名和id,即(getEle ...

  8. sql 传入参数为逗号分隔的字符串处理方法

    写了个存储过程,中间用到了类似这种写法 Select * From User Where ID In('1,2,3') 其中'1,2,3'是从外面传进来的参数,就这样执行报错:'1,2,3'转换为in ...

  9. Oracle 通过出生日期计算年龄

    方法一: SELECT TRUNC(months_between(sysdate, birth)/12) AS age from mytable 方法二: select TRUNC((to_char( ...

  10. [C++]多线程: 教你写第一个线程

    原文:http://blog.csdn.net/cn_wk/article/details/62236057 hello thread! 声明线程A的端口号 #include <pthread. ...