397. 最长上升连续子序列
中文English
给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)
样例
样例 1:
输入:[5, 4, 2, 1, 3]
输出:4
解释:
给定 [5, 4, 2, 1, 3],其最长上升连续子序列(LICS)为 [5, 4, 2, 1],返回 4。
样例 2:
输入:[5, 1, 2, 3, 4]
输出:4
解释:
给定 [5, 1, 2, 3, 4],其最长上升连续子序列(LICS)为 [1, 2, 3, 4],返回 4。
挑战
使用 O(n) 时间和 O(1) 额外空间来解决
class Solution {
public:
int result = 0;
void LICS(vector<int> &A, int n){
int *f = new int[n];
for(int i=0;i<n;i++){
f[i] = 1;
if(i > 0 && A[i - 1] < A[i]){
f[i] = f[i - 1] + 1;
}
if(f[i] > result){
result = f[i];
}
}
}
int longestIncreasingContinuousSubsequence(vector<int> &A) {
int n = A.size();
if(n == 0){
return 0;
}
LICS(A, n);
int i = 0 , j = n - 1 , t;
while(i < j){
t = A[i];
A[i] = A[j];
A[j] = t;
i++;
j--;
}
LICS(A, n);
return result;
}
};