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 as the largest subsequence where the consecutive elements are strictly increasing.
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1, 3, 5] with a length of 3.
def findLengthOfLCIS(nums):
if not nums:
return 0
# Initialize the count and max length
max_length = 1
current_length = 1
# Iterate through the array
for i in range(1, nums.length):
# Check if the current element is greater than the previous element
if nums[i] > nums[i - 1]:
current_length += 1 # Increment current length
else:
max_length = max(max_length, current_length)
current_length = 1 # Reset current length
# After the loop ends, we must check once more to update the max_length.
max_length = max(max_length, current_length)
return max_length
max_length
to track the maximum length of continuous increasing subsequence found.current_length
to track the length of the current continuous increasing subsequence.current_length
.max_length
if current_length
is greater, and reset current_length
to 1.max_length
.This implementation ensures that we’re efficiently finding the length of the longest continuous increasing subsequence with optimal time and space complexity.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?