# Longest Increasing Continuous Subsequence

Easy

## Question

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.

Example
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 .

## Thinking

• Using two array to save sequences.
• Return the longest

## Review

Since we don’t care about the real sequences, we can just use two integers to save the longest value.

## Solution

#### Java (Using two integers)

``````public class Solution {
/**
* @param A an array of Integer
* @return  an integer
*/
public int longestIncreasingContinuousSubsequence(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
if (A.length == 1) {
return 1;
}
int longest = 0;
int tmpLen = 1; // A
boolean isIncreasing = (A <= A);
for (int i = 1; i < A.length; i++) {
if (A[i-1] <= A[i]) {
if (isIncreasing) {
++tmpLen;
} else {
if (tmpLen > longest) {
longest = tmpLen;
}
tmpLen = 2; // A[i-1], A[i]
isIncreasing = true;
}
} else {
if (!isIncreasing) {
++tmpLen;
} else {
if (tmpLen > longest) {
longest = tmpLen;
}
tmpLen = 2;
isIncreasing = false;
}
}
}
return tmpLen > longest ? tmpLen : longest;
}
}
``````

#### Java

``````public class Solution {
/**
* @param A an array of Integer
* @return  an integer
*/
public int longestIncreasingContinuousSubsequence(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
if (A.length == 1) {
return 1;
}
List<Integer> lics = new ArrayList<Integer>();
List<Integer> tmplics = new ArrayList<Integer>();
boolean isIncreasing = A <= A;
int tmp = A;
for (int i = 1; i < A.length; i++) {
if (A[i - 1] <= A[i]) {
if (isIncreasing) {
} else {
if (tmplics.size() > lics.size()) {
lics = tmplics;
}
tmplics = new ArrayList<Integer>();
isIncreasing = true;
}
} else {
if (!isIncreasing) {
} else {
if (tmplics.size() > lics.size()) {
lics = tmplics;
}
tmplics = new ArrayList<Integer>();