LintCode 397: Longest Increasing Continuous Subsequence

题目描述

给定一个整数数组(下标从0n - 1n表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

样例

给定[5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为[5, 4, 2, 1], 返回4.

给定[5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为[1, 2, 3, 4], 返回4.

Thu Feb 23 2017

思路

数组的题目一般会用指针扫描遍历,时间复杂度为\(O(n)\),大多数题目都是这样的套路。

先假设只求单调上升序列,只需要从第一个数字开始向后扫描,遇到更大的数计数器就加一,否则就重新计数。

若是需要同时考虑单调上升和单调下降的话,只需要将判断条件改为当前一对数与前面一对数的单调关系是否相同就行了。

还有一些小细节需要注意,比如若是数组长度小于等于2,则直接返回数组长度。

遇到单调性不一致的数,计数器不是直接归零,而是变为2,因为当前数字与前一个数字肯定是单调上升或单调下降的。

代码

// 最长上升连续子序列
int longestIncreasingContinuousSubsequence(vector<int>& A)
{
if (A.size() <= 2) return A.size();
int now = 2, ans = 2;
for (int i = 2; i < A.size(); ++i)
{
if ((A[i] < A[i - 1]) == (A[i - 1] < A[i - 2]))
ans = ++now > ans ? now : ans;
else
now = 2;
}
return ans; }

LintCode 397: Longest Increasing Continuous Subsequence的更多相关文章

  1. [LintCode] Longest Increasing Continuous subsequence

    http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...

  2. [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

    Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...

  3. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

    [题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...

  4. LintCode "Longest Increasing Continuous subsequence II" !!

    DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...

  5. Longest Increasing Common Subsequence (LICS)

    最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...

  6. 397. Longest Continuous Increasing Subsequence

    Description Give an integer array,find the longest increasing continuous subsequence in this array. ...

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

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

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

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

  9. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

随机推荐

  1. caffe环境搭建笔记

    首先安装以下库或软件 sudo apt-get install gitsudo apt-get install      libprotobuf-dev     libleveldb-dev    l ...

  2. MySQL的并发访问控制(锁)

    前言:任何的数据集只要支持并发访问模型就必须基于锁机制进行访问控制 锁种类 读锁:共享锁,允许给其他人读,不允许他人写写锁:独占锁, 不允许其他人读和写 锁类型 显示锁:用户手动请求读锁或写锁隐式锁: ...

  3. 太平洋网络ip地址查询接口使用,返回json格式,默认返回jsonp

    http://whois.pconline.com.cn/ipJson.jsp?json=true

  4. 我的 MyBatis 实现的 Dao 层

    学了 Mybatis 之后,发现用 Mybatis 写 Dao层实在是简便多了,主要是在表的映射这块简单了很多.下面是我实现的使用 Mybatis 实现的简单的操作用户表的 Dao 层. 使用 Myb ...

  5. SQL Server bit数据类型

    bit值保存为1/0,1代表true,0代表false读取数据库数据时,可以直接用bool型读取该字段,会直接转换为true/false 数据库表结构 CREATE TABLE [dbo].[BitT ...

  6. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  7. BZOJ4976 宝石镶嵌(动态规划)

    显然被留下的宝石应该贡献至少一位,否则就可以扔掉.所以如果n-k>=logw,直接输出所有数的or.现在n变得和k同阶了.于是设f[i][j]为前i个数or为j时至少选几个数,转移显然.当然可以 ...

  8. P1407 [国家集训队]稳定婚姻

    题目描述 我国的离婚率连续7年上升,今年的头两季,平均每天有近5000对夫妇离婚,大城市的离婚率上升最快,有研究婚姻问题的专家认为,是与简化离婚手续有关. 25岁的姗姗和男友谈恋爱半年就结婚,结婚不到 ...

  9. jQuery高度及位置操作

    1. 获取滑轮位置,scrolltop:上下滚动的意思. <!DOCTYPE html> <html lang="en"> <head> < ...

  10. Tajo--一个分布式数据仓库系统(分布式环境安装试用)

    前面两篇介绍了一下tajo,下面就说一下安装和使用吧. 一.分布式安装 前提:hadoop2中的hdfs和yarn已经安装并运行正常. 1.下载source并build源码 $git clone ht ...