给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
注意事项
time
您在真实的面试中是否遇到过这个题? Yes
样例
给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.
给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4.
标签
相关题目
class Solution {
public:
/**
* @param A an array of Integer
* @return an in teger
*/
int longestIncreasingContinuousSubsequence(vector<int>& A) {
// Write your code here
vector<int> dp(A.size(),1),dp2(A.size(),1);int res=1,res2=1;
if(A.size()==0) return 0;
//针对递增序列情况
for(int i=0;i<A.size()-1;++i ){
if(A[i]<A[i+1])
{ dp[i+1]=dp[i]+1;
res=max(res,dp[i+1]);}
//针对递减序列情况
if(A[i]>A[i+1])
{ dp2[i+1]=dp2[i]+1;
res2=max(res2,dp2[i+1]);}
}
return max(res,res2);
}
};