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] <= 109Problem Overview: Given an integer array nums, find the length of the longest strictly increasing continuous subarray. The sequence must use adjacent elements, so you cannot skip indices like in the classic LIS problem.
Approach 1: Sliding Window (O(n) time, O(1) space)
Treat the array as a window that expands while elements remain strictly increasing. Maintain two pointers: start marks the beginning of the current increasing window and i scans through the array. If nums[i] > nums[i-1], the window continues growing. When the order breaks, move start to i because a new increasing segment begins. Track the maximum window length seen during the scan.
This works because a continuous increasing sequence only depends on the relationship between adjacent elements. The moment the condition fails, the previous window cannot extend further. The algorithm performs a single pass over the array, making it efficient for large inputs. This pattern is common in problems involving consecutive segments and appears frequently in sliding window problems.
Approach 2: Greedy Linear Scan (O(n) time, O(1) space)
A simpler variation uses a greedy counter instead of explicit window pointers. Maintain two variables: currentLength for the current increasing run and maxLength for the best result so far. Iterate from index 1 to the end. If nums[i] > nums[i-1], increment currentLength. Otherwise reset it to 1 because the sequence must restart from the current element.
The greedy insight: at each step, extending the current increasing run is always optimal if the condition holds. There is no benefit in restarting earlier or skipping elements because the subsequence must remain continuous. This keeps the logic minimal while still achieving optimal performance. The approach is often discussed alongside basic array traversal patterns and greedy techniques.
Recommended for interviews: The greedy single-pass scan is typically what interviewers expect. It shows you recognize that only adjacent comparisons matter and that the problem reduces to tracking streak lengths. Explaining the sliding window interpretation first can demonstrate problem decomposition, but implementing the greedy version quickly signals strong pattern recognition.
This approach involves iterating through the array while maintaining a variable to track the length of the current continuous increasing subsequence. As we encounter each element, we check if it is larger than the previous one to decide if we should continue the current subsequence or start a new one. We also maintain a global maximum to store the maximum length found during our iteration.
The function findLengthOfLCIS accepts an array and its size. We initialize maxLen and currLen to 1. We iterate through the array, and whenever the current element is greater than the previous one, we increase currLen. If currLen exceeds maxLen, we update maxLen. When the sequence breaks, we reset currLen to 1.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as we use a constant amount of space.
The greedy approach leverages a single pass through the array to determine the maximum length of an increasing subsequence. This method is optimal because it immediately processes each element only once, updating the sequence lengths on the fly, and computing the maximum without revisiting any part of the array.
This code utilizes a greedy solution where it enters the loop with the ability to modify the beginning of sequences implicitly by re-syncing the currLen, ensuring we keep the cost constant per iteration.
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(1), as no additional space other than variables is utilized.
| Approach | Complexity |
|---|---|
| Sliding Window Approach | Time Complexity: O(n), where n is the number of elements in the array. |
| Greedy Approach | Time Complexity: O(n), where n is the number of elements in the array. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sliding Window | O(n) | O(1) | Useful when thinking in terms of continuous segments or adapting to other window-based problems |
| Greedy Linear Scan | O(n) | O(1) | Best general solution; minimal code and optimal performance for interviews |
Sliding Window Approach | Longest Continuous Increasing Subsequence | LeetCode 674. • Nick White • 41,843 views views
Watch 9 more video solutions →Practice Longest Continuous Increasing Subsequence with our built-in code editor and test cases.
Practice on FleetCode