Leetcode 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (LCIS).
A subsequence is continuous if it appears in consecutive indices of the array.
To solve this problem effectively:
maxLength
to keep track of the maximum length of any LCIS found.currentLength
to keep track of the current LCIS length as we iterate through the array.currentLength
.maxLength
if currentLength
is greater.currentLength
to 1 since a new subsequence might begin at the current element.public class Solution {
public int findLengthOfLCIS(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int maxLength = 1;
int currentLength = 1;
// Iterate through the array
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1]) {
currentLength++;
} else {
maxLength = Math.max(maxLength, currentLength);
currentLength = 1;
}
}
// Final check to update maxLength in case the longest LCIS ends at the last element
maxLength = Math.max(maxLength, currentLength);
return maxLength;
}
}
This approach ensures an efficient and clear solution to the problem of finding the longest continuous increasing subsequence.
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?