[LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
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.
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 最长连续递增子序列的更多相关文章
- [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 II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
- LintCode 397: Longest Increasing Continuous Subsequence
LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- pta 习题集 5-5 最长连续递增子序列 (dp)
给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列.例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8). 输入格式: 输入第1行给出正整数n ...
- 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列
题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- Leetcode674.Longest Continuous Increasing Subsequence最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
随机推荐
- 等号赋值与memcpy的效率问题
转自:http://www.aiuxian.com/article/p-1309055.html 偶尔看到一个说法,说,小内存的拷贝,使用等号直接赋值比memcpy快得多.结合自己搜集到的资料,整理成 ...
- Shell脚本获取C语言可执行程序返回值
#!/bin/sh #./test是c程序,该程序 返回0 ./test OP_MODE=$? echo $OP_MODE # $? 显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误.
- 配置ogg异构oracle-mysql(1)基础环境配置
一.环境描述: 192.168.0.164 ( Oracle ) —> 192.168.0.165 (Mysql ) 版本: 操作系统:redhat5.8 Oracle: 11.2.0.3 M ...
- Fragment实现兼容手机和平板
Android手机的设置界面,点击一下Sound,可以跳转到声音设置界面,如下面两张图所示: 然后再来看一下Android Pad的设置界面,主设置页面和声音设置页面都是在一个 ...
- ThinkPHP去重 distinct和group by
转自:http://blog.csdn.net/helencoder/article/details/50328629 近期项目中,遇到数据表去重要求,对于ThinkPHP的去重有了更加准确的认识和体 ...
- Javascript参考手册
---------------------------------------------------------------------------------------------------- ...
- 如何解决在Ue4编辑器中查看中文注释为乱码的情况
一般人都会在自己定义的函数后面添加注释,Ue4会在蓝图编辑器中显示这些注释,这是一个相当棒的设定. 但是如果这些注释是中文的话,在蓝图编辑器中就会显示乱码. 如何解决呢? 只需要把你的文件用UTF-8 ...
- My97DatePicker控件
本文只做了功能说明,详细请看http://www.my97.net/dp/demo/index.htm 二. 功能及示例 1. 常规功能 支持多种调用模式 除了支持常规在input单击或获得焦点调用外 ...
- Visual Studio: Show Solution Platform in Toolbar
link: http://stackoverflow.com/questions/7516755/solution-configuration-but-not-platform-in-vs2010-t ...
- BZOJ3566 : [SHOI2014]概率充电器
选个根把无根树转化成有根树, 设f[i]表示i不通电的概率 则 答案为对于枚举树根root进行DP后1-f[root]的和 直接算是O(n^2)的,但是n有500000,所以不能过. 对于这样一棵以1 ...