Tuesday, April 25, 2017

Longest Increasing Continuous Subsequence

public class Solution {
    /**
     * @param A an array of Integer
     * @return  an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // Write your code here
        if (A == null || A.length == 0) return 0;
        int increase = 1, decrease = 1;
        int max = 1;
        for (int i = 1; i < A.length; i++) {
            if (A[i] > A[i - 1]) {
                increase++;
                decrease = 1;
            } else if (A[i] < A[i - 1]) {
                decrease++;
                increase = 1;
            } else {
                decrease = 1;
                increase = 1;
            }
            max = Math.max(max, Math.max(increase, decrease));
        }
        return max;
    }
}

No comments:

Post a Comment