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 ...
随机推荐
- 分享几个IP获取地理位置的API接口(最全面的了)
转载;https://cloud.tencent.com/developer/article/1152362 全网首发,最全的IP接口,不服来辩!博主找了几个小时的资料,又手动抓取到了几个接口补充进来 ...
- ASP.NET前后端分离框架
- jstack笔记
遇到java程序跑不动怎么办,jstack是比较容易想到的一个工具,利用jstack来dump出一个线程堆栈快照,然后具体分析. 一般的堆栈大概是由下面的部分组成的: "resin-2212 ...
- Java 调用 google 翻译
1.Java代码 public class Translator { public String translate(String langFrom, String langTo, String wo ...
- 九度-题目1195:最长&最短文本
http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...
- 某客的《微信小程序》从基础到实战视频教程
第 1 部分 微信小程序从基础到实战课程概要 第 1 节 微信小程序从基础到实战课程概要 1.1微信小程序从基础到实战课程概要 第 2 部分 初识微信小程序 第 1 节 微信小程序简 ...
- 第89天:HTML5中 访问历史、全屏和网页存储API
一.访问历史 API 通过history对象实现前进.后退和刷新之类的操作 history新增的两个方法history.replaceState()和history.pushState()方法属于HT ...
- 【ASP.NET 框架系列】您所经历的,但未必研究的那些技术
本篇文章更适合具有一定开发经验,一定功底,且对底层代码有所研究的朋友!!! 本篇文章稍微偏原理且底层,有一定难度和且比较晦涩,文章粒度稍微粗些,更细粒度的,会在后续的文章中,结合具体的Demo实 ...
- Win10 安装 Linux 子系统
Win10 安装 Linux 子系统 因为最近要使用Linux搭服务器,但是用远程的话延迟很烦,用双系统切换很麻烦,用虚拟机又会有点卡,刚好Windows10最近更新了正式版的WSL(windows下 ...
- oracle job定时执行存储过程
JOB定时跑插入语句1.建插入数据的存储过程create or replace procedure report_web asV_START_DATE DATE;V_END_DATE DATE;b ...