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

An increasing continuous subsequence:

  • Can be from right to left or from left to right.
  • Indices of the integers in the subsequence should be continuous.
Notice

O(n) time and O(1) extra space.

Have you met this question in a real interview?

Yes
Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

这道题跟LeetCode上那道Longest Increasing Subsequence很像,但是比那道题简单,因为这道题需要递增子序列连续,这样我们只要挨个比较一下即可,由于题目中说了左右两个方向递增都行,可以用左右两个方向分别遍历一遍,也可以把两个遍历合并到一起,只用一个循环,同时寻找递增和递减的数列,使用两个变量cnt1和cnt2分别记录最长递增和递减数列的长度,一旦递增递减断开,记得将对应的计数器重置即可,参见代码如下:

class Solution {
public:
/**
* @param A an array of Integer
* @return an integer
*/
int longestIncreasingContinuousSubsequence(vector<int>& A) {
if (A.empty()) return ;
int res = , cnt1 = , cnt2 = ;
for (int i = ; i < A.size() - ; ++i) {
if (A[i] < A[i + ]) {
++cnt1;
cnt2 = ;
} else {
++cnt2;
cnt1 = ;
}
res = max(res, max(cnt1, cnt2));
}
return res;
}
};

类似题目:

Longest Increasing Subsequence

[LintCode] 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 II" !!

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

  3. LintCode 397: Longest Increasing Continuous Subsequence

    LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...

  4. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

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

  5. pta 习题集 5-5 最长连续递增子序列 (dp)

    给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列.例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8). 输入格式: 输入第1行给出正整数n ...

  6. 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列

    题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...

  7. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  8. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  9. Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列

    给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...

随机推荐

  1. WebView相关

    Android WebView使用基础 Android WebView中的JavaScript代码使用 很不错的例子:android webview js交互 第一节 (java和js交互)

  2. 外键为','(逗号)拼接ID,连接查询外键表ID

    select distinct pipeId=substring(a.PipeIn,b.number,charindex(',',a.PipeIn+',',b.number)-b.number) fr ...

  3. ios广告

    ios广告只需要添加iAd.framework框架 添加广告控件ADBannerView,在控制器中设置广告控件代理<ADBannerViewDelegate>即可,广告会有苹果官方自动推 ...

  4. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  5. Android 蹲坑的疑难杂症集锦一

    各位看官老爷子你们好,我就是那个挖坑不埋,还喜欢开新矿的小喵同志. 问大家一个问题,在Github上找项目的时候,看到中文简介说明你们是不是觉得这个项目很low不屑一顾? 最近朋友无意中说,在Gith ...

  6. CentOS升级Python 2.6到2.7

    查看python的版本 python -V Python 2.6.6 下载Python   Python-2.7.8.tar.xz      链接:http://pan.baidu.com/s/1i4 ...

  7. 廖雪峰js教程笔记9 json

    JSON是JavaScript Object Notation的缩写,它是一种数据交换格式. 在JSON出现之前,大家一直用XML来传递数据.因为XML是一种纯文本格式,所以它适合在网络上交换数据.X ...

  8. 领域模型(domain model)&贫血模型(anaemic domain model)&充血模型(rich domain model)

    领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...

  9. css:删除:×的效果

    常常要使用的显示删除效果: DEMO

  10. PHP 字符检测自定义函数

    <?php /** * 转义字符替换 * * @param string $subject * @return string */public static function sReplace( ...