[LintCode] Longest Increasing Continuous Subsequence - Road to Glory
题目:最长连续递增/递减子序列
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
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.
思路:
设置正向计数器,后一位增加则计数器加1,否则置1。反向计数器亦然。
每一次比较后将较大值存入max。
public class Solution { public int longestIncreasingContinuousSubsequence(int[] A) { // Write your code here int len = A.length; if (A == null || len == 0) {return 0; } if (A.length == 1) return 1; int count = 1, countn = 1; int i = 1; int max = 0; while (i < len) {if (A[i] >= A[i-1]) { count++; countn = 1; max = Math.max(max, count);}else { count = 1; countn++; max = Math.max(max, countn);}i++; } return max; }}