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. lintcode-389-判断数独是否合法

    389-判断数独是否合法 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项 一个合法的数独(仅部分填充)并不一定是可解的.我们仅需使填充的空格有效即可. 说 ...

  2. Spark Shuffle之Hash Shuffle

    源文件放在github,如有谬误之处,欢迎指正.原文链接https://github.com/jacksu/utils4s/blob/master/spark-knowledge/md/hash-sh ...

  3. Scrum Meeting Beta - 3

    Scrum Meeting Beta - 3 NewTeam 2017/12/1 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成布局方面的界面优化Issue #125 李奕 ...

  4. ant 安装及基础教程 !

    这篇文章主要介绍了ant使用指南详细入门教程,本文详细的讲解了安装.验证安装.使用方法.使用实例.ant命令等内容,需要的朋友可以参考下   一.概述 ant 是一个将软件编译.测试.部署等步骤联系在 ...

  5. c语言基础笔记

    一 :数据类型 1.float类型,在输出的时候可以使用 .数字  来把浮点数精确到小数点后几位,比如 printf("%.3f",float)精确到小数点后三位,不足补0 2.字 ...

  6. apache常用的两种工作模式 prefork和worker

    apache作为现今web服务器用的最广泛也是最稳定的开源服务器软件,其工作模式有许多中,目前主要有两种模式:prefork模式和worker模式 一.两种模式 prefork模式: prefork是 ...

  7. Binlog的三种模式

    binlog模式分三种(row,statement,mixed) 1.Row 日志中会记录成每一行数据被修改的形式,然后在slave端再对相同的数据进行修改,只记录要修改的数据,只有value,不会有 ...

  8. idea Class<>表示的含义

  9. 【bzoj4007】[JLOI2015]战争调度 暴力+树形背包dp

    题目描述 给你一棵 $n$ 层的完全二叉树,每个节点可以染黑白两种颜色.对于每个叶子节点及其某个祖先节点,如果它们均为黑色则有一个贡献值,如果均为白色则有另一个贡献值.要求黑色的叶子节点数目不超过 $ ...

  10. 【bzoj5130】[Lydsy12月赛]字符串的周期 DFS+KMP

    题目描述 给定 $n$ 和 $m$ ,求所有 长度为 $n$ ,字符集大小为 $m$ 的字符串,每个前缀的最短循环节长度乘积 的总和. $n\le 12,m\le 10^9$ 题解 DFS+KMP 对 ...