Description
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.
Example
Example 1:
Input: [5, 4, 2, 1, 3]
Output: 4
Explanation:
For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.
Example 2:
Input: [5, 1, 2, 3, 4] Output: 4 Explanation: For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.
思路:本题为直接去找一个数组的最长连续上升/下降子序列,直接从左往右枚举即可,每次到单调性变化的时候更新答案。
public class Solution { public int longestIncreasingContinuousSubsequence(int[] A) { if (A == null || A.length == 0) { return 0; } int n = A.length; int answer = 1; // from left to right int length = 1; // just A[0] itself for (int i = 1; i < n; i++) { if (A[i] > A[i - 1]) { length++; } else { length = 1; } answer = Math.max(answer, length); } // from right to left length = 1; for (int i = n - 2; i >= 0; i--) { if (A[i] > A[i + 1]) { length++; } else { length = 1; } answer = Math.max(answer, length); } return answer; } }
Challenge
O(n) time and O(1) extra space.