Lintcode-动态规划-最长上升连续子序列
来源:互联网 发布:sql 恢复删除数据 编辑:程序博客网 时间:2024/05/21 20:27
点击进入题目
解题思路:这道题目是让求一串数组里面,最长的上升子数组和下降子数组的长度。那么重点就是求上升子数组和下降子数组了,那么就可以用循环的方式求这个问题。
解题过程:给定一个数组A,定义两个数组In(下降子数组),De(上升子数组),定义整形max=0,然后嵌套循环,要么求出来In,要么求出来De。令max=max和In(或者De长度)的最大值。
代码实现:
int max(int i,int j)
{
return i<j?j:i;
}
int longestIncreasingContinuousSubsequence(vector<int> &A) {
// write your code here
vector<int>In,De;
int Max=0;
for(int i=0;i<A.size();i++)
{
{
return i<j?j:i;
}
int longestIncreasingContinuousSubsequence(vector<int> &A) {
// write your code here
vector<int>In,De;
int Max=0;
for(int i=0;i<A.size();i++)
{
int count=0;
int I=1,D=1;
In.clear();De.clear();
In.push_back(A[i]);
De.push_back(A[i]);
for(int j=i+1;j<A.size();j++)
{
if(A[j-1]<A[j])
{
I=0;
if(D)
{
De.push_back(A[j]);
count=1;
i++;
}
}
else if(A[j-1]>A[j])
{
D=0;
if(I)
{
In.push_back(A[j]);
count=1;
i++;
}
int I=1,D=1;
In.clear();De.clear();
In.push_back(A[i]);
De.push_back(A[i]);
for(int j=i+1;j<A.size();j++)
{
if(A[j-1]<A[j])
{
I=0;
if(D)
{
De.push_back(A[j]);
count=1;
i++;
}
}
else if(A[j-1]>A[j])
{
D=0;
if(I)
{
In.push_back(A[j]);
count=1;
i++;
}
}
}
Max=max(Max,In.size());
Max=max(Max,De.size());
if(count) i--;
}
return Max;
}
}
Max=max(Max,In.size());
Max=max(Max,De.size());
if(count) i--;
}
return Max;
}
注意事项:要注意In和De在同一时间只能求一个,原因很简单,而且每次循环要重新确定i的位置,如果仅仅令i++的话,会扩大时间复杂度。
个人见解:这道题并不是很复杂,主要就是注意上升子序列和下降子序列而已。
阅读全文
0 0
- Lintcode-动态规划-最长上升连续子序列
- LintCode--最长上升连续子序列
- lintcode-最长上升连续子序列-397
- 最长上升连续子序列(LintCode)
- LintCode- 最长上升连续子序列
- lintCode刷题--最长上升连续子序列
- LintCode:最长上升连续子序列
- lintcode -- 最长上升连续子序列
- lintcode:最长上升连续子序列
- LintCode: 最长上升连续子序列
- Lintcode最长上升连续子序列
- 最长上升连续子序列-LintCode
- 最长上升连续子序列-LintCode
- LintCode 最长上升连续子序列
- lintcode397最长上升连续子序列(动态规划easy)
- 最长不连续上升序列----动态规划
- 动态规划:最长上升子序列
- 动态规划-最长上升子序列LIS
- 大型网站的灵魂——性能
- UDP与TCP穿洞技术学习笔记
- 2311: [CEOI2016]D2T3 Router
- 读书笔记2 MYSQL必知必会
- ssh原理与运用
- Lintcode-动态规划-最长上升连续子序列
- Servlet生命周期
- 51nod 1592 数列积 莫队算法+树状数组
- springboot入门
- SVN与CVS
- strcpy库函数的实现。。
- Android客户端.So 精简策略
- c语言简易学生管理系统
- [noip2012]Vigenère 密码 题解