LintCode 397: Longest Increasing Continuous Subsequence
LintCode 397: Longest Increasing Continuous Subsequence
题目描述
给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
样例
给定[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的更多相关文章
- [LintCode] Longest Increasing Continuous subsequence
http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- LintCode "Longest Increasing Continuous subsequence II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
- Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...
- 397. Longest Continuous Increasing Subsequence
Description Give an integer array,find the longest increasing continuous subsequence in this array. ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
随机推荐
- web压力测试_(收集)
作者:ZeldaZzz链接:http://www.zhihu.com/question/19867883/answer/89775858来源:知乎著作权归作者所有,转载请联系作者获得授权. 一个完整的 ...
- caffe神经网络模型的绘图
Python/draw_net.py, 这个文件,就是用来绘制网络模型的.也就是将网络模型由prototxt变成一张图片. 1.安装GraphViz # sudo apt-get install Gr ...
- phaser入手
做phaser小程序,必须先把环境弄好 发现怎么导入都无济于事. 最后决定亲自操刀,在原代码中,引入全局变量.
- bootstrap练习制作网页
导航条 <nav class="navbar navbar-default"> <div class="container-fluid"> ...
- hdu 6375 百度之星 度度熊学队列
题目链接 Problem Description 度度熊正在学习双端队列,他对其翻转和合并产生了很大的兴趣. 初始时有 N 个空的双端队列(编号为 1 到 N ),你要支持度度熊的 Q 次操作. ①1 ...
- poj3107 Godfather 求树的重心
Description Last years Chicago was full of gangster fights and strange murders. The chief of the pol ...
- CGLib动态代理引起的空指针异常
一个同事将公司的开发框架基于最新的Spring.Tomcat.Java版本作了部分修改,拿来开发运行之后,发现一个奇怪的空指针异常. 还原一下当时的场景,代码大概如下,所有的Servlet继承自Bas ...
- 详解SQL Server数据修复命令DBCC的使用
严重级别为 21 表示可能存在数据损坏. 可能的原因包括损坏的页链.损坏的 IAM 或该对象的 sys.objects目录视图中存在无效条目. 这些错误通常由硬件或磁盘设备驱动程序故障而引起. MS ...
- C++解析(14):静态成员变量与静态成员函数
0.目录 1.静态成员变量 2.静态成员函数 3.小结 1.静态成员变量 成员变量的回顾: 通过对象名能够访问public成员变量 每个对象的成员变量都是专属的 成员变量不能在对象之间共享 新的需求: ...
- 【刷题】BZOJ 4566 [Haoi2016]找相同字符
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别为 ...