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 ...
随机推荐
- 用Python实现求Fibonacci数列的第n项
1. 背景——Fabonacci数列的介绍(摘自百度百科): 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacc ...
- 数据输出保存生成word文档
ob_start(); //打开缓冲区 $header_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office&qu ...
- Java中TimeZone类的常用方法
一.TimeZone类的定义 TimeZone类是一个抽象类,主要包含了对于时区的各种操作,可以进行计算时间偏移量或夏令时等操作 二.TimeZone类的常用方法 1.getAvailableIDs( ...
- Building microservices with ASP.NET Core (without MVC)(转)
There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite a ...
- 拓展kmp总结
借鉴自:https://blog.csdn.net/dyx404514/article/details/41831947 定义母串S,和子串T,设S的长度为n,T的长度为m,求T与S的每一个后缀的最长 ...
- 51nod1222 最小公倍数计数 莫比乌斯反演 数学
求$\sum_{i = 1}^{n} \sum_{j = 1}^{i} [lcm(i, j) \le n]$因为这样不好求,我们改成求$\sum_{i = 1}^{n} \sum_{j = 1}^{n ...
- Linux实用命令行
对于Linux命令,我在学习和使用过程中是有一个循序渐进的过程的.适合小白学习快速使用.大笑 跳转目录:cd +路径 例如:cd /home/workspace 查看某个文件,常用的是查看日志:tai ...
- Android 通过浏览器打开应用
在很多应用的web站,其实都有这样一个功能,就是直接在网页中打开应用,接下来的就来探讨一下这个功能的实现,有些地方也我还没弄明白,还请懂的大神指点. 首先,得说一点不好消息,在微信中,这样的方式是行不 ...
- 【BZOJ1042】硬币购物(动态规划,容斥原理)
[BZOJ1042]硬币购物(动态规划,容斥原理) 题面 BZOJ Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬 ...
- 被动式pocscan扫描神器搭建
1.搭建环境: 操作系统为:ubuntu16.04 x64位系统,内核版本3.0.10以上 2.安装docker镜像 root@backlion-virtual-machine:/# apt-get ...