class Solution:
def findLengthOfLCIS(self, nums):
if not nums:
return 0
max_len = 1
curr_len = 1
for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
curr_len += 1
max_len = max(max_len, curr_len)
else:
curr_len = 1
return max_len
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
if (nums.empty()) return 0;
int maxLen = 1, currLen = 1;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > nums[i-1]) {
currLen++;
maxLen = max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}
};
class Solution {
public int findLengthOfLCIS(int[] nums) {
if (nums.length == 0) return 0;
int maxLen = 1, currLen = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
currLen++;
maxLen = Math.max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
}
}
var findLengthOfLCIS = function(nums) {
if (nums.length === 0) return 0;
let maxLen = 1, currLen = 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
currLen++;
maxLen = Math.max(maxLen, currLen);
} else {
currLen = 1;
}
}
return maxLen;
};
Given an array of integers nums
, your task is to find the length of the longest continuous increasing subsequence (LCIS) in the array. A continuous increasing subsequence is a sequence of elements from nums
that are strictly increasing and are also consecutive in the array (i.e., no elements are skipped).
nums
is empty, return 0
.To solve this problem, let's first think about what it means to have a "longest continuous increasing subsequence." We need to find the longest stretch in the array where each number is bigger than the one before it, and all numbers are next to each other.
The brute-force way would be to check every possible subarray, but that's inefficient, especially for large arrays. Instead, we can "scan" the array from left to right, keeping track of the current increasing streak. If the streak breaks (the next number is not larger), we reset our counter. Throughout, we keep track of the maximum streak we've seen so far.
This is similar to counting the longest run of heads in a sequence of coin tosses—you only need to remember the current run and the best run.
Let's break down the steps to solve the problem efficiently:
maxLen
) to keep track of the longest increasing subsequence found so far. Initialize it to 1 (since any single element is an increasing subsequence).currLen
) to count the length of the current increasing subsequence. Also start at 1.currLen
by 1.currLen
to 1 (since the streak is broken).maxLen
if currLen
is greater than maxLen
.maxLen
.This approach only requires a single pass through the array and uses constant extra space.
Let's consider the input nums = [1, 3, 5, 4, 7]
.
maxLen = 1
, currLen = 1
currLen = 2
, maxLen = 2
currLen = 3
, maxLen = 3
currLen = 1
currLen = 2
(but maxLen
remains 3)The longest continuous increasing subsequence is [1, 3, 5], with a length of 3. The function returns 3.
The key to solving the Longest Continuous Increasing Subsequence problem is recognizing that we can process the array in a single pass, tracking only the current streak and the maximum found so far. This approach is both time and space efficient, making it suitable for large inputs. By resetting the current streak whenever the increasing property breaks, we ensure that only continuous sequences are considered, leading to a simple and elegant solution.