Problem Description
Given an unsorted array of integers, return the length of the longest continuous strictly increasing subsequence (subarray). A continuous increasing subsequence is defined by a range [l, r] where for every index i from l to r-1, nums[i] < nums[i+1].
Key Insights
- The subsequence must be continuous and strictly increasing.
- A single element is considered an increasing subsequence of length 1.
- Use a single scan through the array to track the length of the current increasing segment.
- If the current element is greater than the previous one, extend the current segment; otherwise, reset the segment length.
Space and Time Complexity
Time Complexity: O(n) - where n is the number of elements in the array, since we traverse the list once.
Space Complexity: O(1) - constant extra space is used.
Solution
We traverse the array while maintaining two variables: one for the current length of the increasing segment and one for the maximum length found so far.
- Start by initializing both counters to 1 because a single element is a valid subsequence.
- For each element from the second element onward, compare it to the previous element.
- If the current element is greater than the previous one, increment the current segment length.
- If not, update the maximum length if the current segment is longer, then reset the current segment length to 1.
- After the loop, ensure to update the maximum length one last time in case the longest segment is at the array end.