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. 撩课-Java每天10道面试题第1天

    1.简述JDK.JRE.JVM? 一.JDK JDK(Java Development Kit) 是整个JAVA的核心, 包括了Java运行环境(Java Runtime Envirnment), 一 ...

  2. python 实现websocket

    python中websocket需要我们自己实现握手代码,流程是这样:服务端启动websocket服务,并监听.当客户端连接过来时,(需要我们自己实现)服务端就接收客户端的请求数据,拿到请求头,根据请 ...

  3. 中小型研发团队架构实践三:微服务架构(MSA)

    一.MSA 简介 1.1.MSA 是什么 微服务架构 MSA 是 Microservice Architect 的简称,它是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相通讯.互相 ...

  4. 使用fastjson 获取json字符串中的数组,再转化为java集合对象

    最近,使用layui做一个导出功能,尽管有插件提供导出,但是呢,由于数据中有状态是数字,例如1显示是已支付,但是导出时也希望这样显示,但是导出的还是1: lz没有找到改下这个插件的办法,只能利用服务端 ...

  5. Bzoj3597: [Scoi2014]方伯伯运椰子

    题面 传送门 Sol 消圈定理:如果一个费用流网络的残量网络有负环,那么这个费用流不优 于是这个题就可以建出残量网络,然后分数规划跑负环了 # include <bits/stdc++.h> ...

  6. (C#) 多线程访问探讨,如果保证线程安全?

    先抛出几点疑问: 1. 多个线程同时访问同一个“值类型变量“(value type, stored in stack), 如果保证安全访问? 2. 多个线程同时访问同一个“引用类型变量“(refere ...

  7. How to save rules of the iptables?

    The easy way is to use iptables-persistent. Install iptables-persistent: sudo apt-get install iptabl ...

  8. BAT脚本编写教程简单入门篇

    BAT脚本编写教程简单入门篇 批处理文件最常用的几个命令: echo表示显示此命令后的字符 echo on  表示在此语句后所有运行的命令都显示命令行本身 echo off 表示在此语句后所有运行的命 ...

  9. RAP, 高效前后端联调框架,接口文档管理工具

    RAP通过GUI工具帮助WEB工程师更高效的管理接口文档,同时通过分析接口结构自动生成Mock数据.校验真实接口的正确性,使接口文档成为开发流程中的强依赖.有了结构化的API数据,RAP可以做的更多, ...

  10. d3js data joins深入理解

    Data joins 给定一个数据数组和一个 D3 selection  我们就可以attach或者说是'join'数组中的每个数据到selection中的每个元素上. 这将使得我们的数据和可视化元素 ...