Given an unsorted array of integers nums, return the length of the longestcontinuous 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].
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.
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.
We can find the longest continuous increasing subsequence by scanning the array once, keeping track of the current increasing segment and updating the maximum length found so far.
classSolution {
public:int findLengthOfLCIS(vector<int>& nums) {
int ans =1, cur =1;
for (int i =1; i < nums.size(); ++i) {
if (nums[i] > nums[i-1]) cur++;
else cur =1;
ans = max(ans, cur);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcfindLengthOfLCIS(nums []int) int {
ans, cur:=1, 1fori:=1; i < len(nums); i++ {
ifnums[i] > nums[i-1] {
cur++ } else {
cur = 1 }
ifcur > ans {
ans = cur }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
classSolution {
publicintfindLengthOfLCIS(int[] nums) {
int ans = 1, cur = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i]> nums[i-1]) cur++;
else cur = 1;
ans = Math.max(ans, cur);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funfindLengthOfLCIS(nums: IntArray): Int {
var ans = 1var cur = 1for (i in1 until nums.size) {
if (nums[i] > nums[i-1]) cur++else cur = 1 ans = maxOf(ans, cur)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution:
deffindLengthOfLCIS(self, nums: list[int]) -> int:
ans = cur =1for i in range(1, len(nums)):
if nums[i] > nums[i-1]:
cur +=1else:
cur =1 ans = max(ans, cur)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnfind_length_of_lcis(nums: Vec<i32>) -> i32 {
letmut ans =1;
letmut cur =1;
for i in1..nums.len() {
if nums[i] > nums[i-1] {
cur +=1;
} else {
cur =1;
}
ans = ans.max(cur);
}
ans
}
}