Longest Continuous Increasing Subsequence
Problem:
Given an unsorted array of integers, find the length of longest continuous increasing subsequence.
- Note:
Length of the array will not exceed 10,000.
- Examples:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
- Analysis:
It's equal to the longest increasing subarry.
- Code - Java:
class Solution {
public int findLengthOfLCIS(int[] nums) {
int len = nums.length;
int left = 0;
int result = 0;
for (int i = 1; i < len; i++) {
if (nums[i - 1] >= nums[i]) {
result = Math.max(i - left, result);
left = i;
}
}
result = Math.max(len - left, result);
return result;
}
}
- Code - C++:
public:
int findLengthOfLCIS(vector<int>& nums) {
int size = nums.size();
int left = 0;
int result = 0;
for (int i = 1; i < size; i++) {
if (nums[i - 1] >= nums[i]) {
result = max(i - left, result);
left = i;
}
}
result = max(size - left, result);
return result;
}
};
- Code - Python:
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
size = len(nums)
left, result = 0, 0
for i in range(1, size):
if nums[i - 1] >= nums[i]:
result = max(i - left, result)
left = i
return max(size - left, result)
- Code - C:
int findLengthOfLCIS(int* nums, int numsSize) {
int left = 0;
int result = 0;
for (int i = 1; i < numsSize; i++) {
if (nums[i - 1] >= nums[i]) {
if (i - left > result) {
result = i - left;
}
left = i;
}
}
if (numsSize - left > result) {
result = numsSize - left;
}
return result;
}
Time Complexity: O(N), N is the length of input array
Space Complexity: O(1)