{x}
blog image

Longest Continuous Increasing Subsequence

Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.

A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].

 

Example 1:

Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.

Example 2:

Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.

 

Constraints:

  • 1 <= nums.length <= 104
  • -109 <= nums[i] <= 109

Solution Explanation for Longest Continuous Increasing Subsequence

The problem asks to find the length of the longest continuous increasing subsequence within a given array of integers. A continuous increasing subsequence is a subarray where each element is strictly greater than the previous one.

Approach 1: One-Pass Scan

This approach iterates through the array only once, maintaining a count (cnt) of the current increasing subsequence's length.

  1. Initialization: ans (the maximum length found so far) and cnt are initialized to 1. We start with a subsequence length of 1 because each element itself is a subsequence of length 1.

  2. Iteration: The code iterates through the array starting from the second element (nums[1:]).

  3. Comparison: For each element, it compares it with the previous element (nums[i-1]).

    • If nums[i-1] < nums[i], the subsequence continues to increase. cnt is incremented, and ans is updated to the maximum of ans and cnt.
    • If nums[i-1] >= nums[i], the increasing subsequence breaks. cnt is reset to 1, starting a new subsequence from the current element.
  4. Result: After iterating through the entire array, ans holds the length of the longest continuous increasing subsequence.

Time Complexity: O(n), as we iterate through the array once. Space Complexity: O(1), as we use only a few constant extra variables.

Approach 2: Two Pointers

This approach uses two pointers, i and j, to define the start and end of a continuous increasing subsequence.

  1. Initialization: ans is initialized to 1 (minimum possible length). i starts at index 0.

  2. Outer Loop: The outer loop iterates while i is within the array bounds.

  3. Inner Loop: The inner loop (using j) extends as long as the subsequence continues to increase (nums[j-1] < nums[j]).

  4. Update: When the inner loop terminates, j - i gives the length of the current increasing subsequence. ans is updated to the maximum of ans and j - i.

  5. Advance: i is updated to j, moving to the beginning of the next potential subsequence.

  6. Result: After the outer loop completes, ans contains the length of the longest continuous increasing subsequence.

Time Complexity: O(n), because although there are nested loops, the inner loop iterates over each element at most once in total. Space Complexity: O(1), as only constant extra space is used.

Code Examples (Python):

Approach 1:

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        ans = cnt = 1
        for i, x in enumerate(nums[1:]):
            if nums[i] < x:
                cnt += 1
                ans = max(ans, cnt)
            else:
                cnt = 1
        return ans

Approach 2:

class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        ans, n = 1, len(nums)
        i = 0
        while i < n:
            j = i + 1
            while j < n and nums[j - 1] < nums[j]:
                j += 1
            ans = max(ans, j - i)
            i = j
        return ans

Both approaches achieve the same result with the same time and space complexities. The one-pass scan might be slightly more concise, but the two-pointer approach can be easier to understand for some. The choice depends on personal preference and coding style. The provided solutions include multiple languages, demonstrating the flexibility and adaptability of the approaches.