Longest Continuous Increasing Subsequence

Problem

Find the length of the longest contiguous increasing subarray (each element strictly greater than the one before it).

Input:  nums = [1,3,5,4,7]
Output: 3   ([1,3,5])

Python Solution

def find_length_of_lcis(nums):
    best = current = 1
    for i in range(1, len(nums)):
        if nums[i] > nums[i - 1]:
            current += 1
        else:
            current = 1
        best = max(best, current)
    return best

C++ Solution

int findLengthOfLCIS(vector<int>& nums) {
    int best = 1, current = 1;
    for (int i = 1; i < nums.size(); i++) {
        current = (nums[i] > nums[i - 1]) ? current + 1 : 1;
        best = max(best, current);
    }
    return best;
}

Java Solution

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int best = 1, current = 1;
        for (int i = 1; i < nums.length; i++) {
            current = (nums[i] > nums[i - 1]) ? current + 1 : 1;
            best = Math.max(best, current);
        }
        return best;
    }
}

Complexity

Time: O(n) — one pass, comparing each element to the one before it. Space: O(1) — two running counters only. This is a contiguous variant, simpler than the Longest Increasing Subsequence DP problem, which allows gaps.

Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.