Longest Increasing Continuous Subsequence

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.

Idea

This kind of question is really tricky. It asks you to find something in the problem statement. But the final answer you have to return is often simpler. For this question, seems that it asks you to find a sequence, while in fact it just asks for the length of the sequence. So there is no need to really remember what the sequence is as long as you count it correctly.

How to count? Just scan the array linearly and increment two counters (two, one for left->right, one for right->left ), if you encounter an element which violates the continuous order, reset the respective counter. You also have to bear in mind that duplicate elements are considered as violating the order.

Here I provide two solutions, in Java and JS respectively. I personally dislike counter related logic very much and would not like to see it inside a loop. So I would prefer wrapping it in a class as I did in JS version.

Java Version

/**
 *  The question says the sequence can be in ascending order from either left to right or right to left.
 *  In another word, the sequence has to be either increasing/decreasing from left to right.
 */
public class Solution {
    /**
     * @param A: An array of Integer
     * @return: an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {

        int max = 0;
        int increasingCount = 0;
        int decreasingCount = 0;
        for(int i = 0; i < A.length; i++) {

            if (i == 0) {
                increasingCount++;
                decreasingCount++;
                max = Math.max(increasingCount, decreasingCount);
                continue;
            }

            if (A[i - 1] < A[i]) {
                increasingCount++;
                decreasingCount = 1;
            } else if (A[i - 1] > A[i]) {
                increasingCount = 1;
                decreasingCount++;
            } else {
                increasingCount = 1;
                decreasingCount = 1;
            }

            max = Math.max(max, decreasingCount);
            max = Math.max(max, increasingCount);
        }

        return max;
    }
}

JS Version

/**
 * @param A: An array of Integer
 * @return: an integer
 */
const longestIncreasingContinuousSubsequence = function (A) {
    const incrCheck = trendChecker((prev, x) => prev < x);
    const decrCheck = trendChecker((prev, x) => prev > x);
    for(let i = 0; i < A.length; i++) {
        incrCheck.check(A[i]);
        decrCheck.check(A[i]);
    }
    return Math.max(incrCheck.max(), decrCheck.max());
}

const trendChecker = (trendCheck) => {
    let count = 0;
    let maxCount = 0;
    let prev = 0;
    const check = (x) => {
        if (count === 0) {
            prev = x;
            count++;
            maxCount = Math.max(count, maxCount);
            return;
        }
        if (trendCheck(prev, x)) {
            count++;
        } else {
            count = 1;
        }
        prev = x;
        maxCount = Math.max(count, maxCount);
    }
    return {
        max: () => maxCount,
        check
    }
}

你可能感兴趣的:(Longest Increasing Continuous Subsequence)