Leetcode 674. The longest continuous increasing sequence
Question stem
Given an unsorted array of integers, find the longest and continuously increasing subsequence, and return the length of the sequence.
The continuously increasing subsequence can be determined by two subscripts l and r (l <r). If for each l <= i <r, there is nums[i] <nums[i + 1], then the subsequence [ nums[l], nums[l + 1], …, nums[r-1], nums[r]] are successively increasing subsequences.
Example 1:
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing sequence is [1,3,5], and the length is 3.
Although [1,3,5,7] is also a subsequence in ascending order, it is not continuous because 5 and 7 are separated by 4 in the original array.
Example 2:
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing sequence is [2], and the length is 1.
提示:
0 <= nums.length <= 104
-109 <= nums[i] <= 109
answer
Iterate and compare
class Solution {
public:
int findLengthOfLCIS(vector<int>& nums) {
int n = nums.size();
if(n == 0){
return 0;
}else if(n == 1){
return 1;
}
int lcisLength = 1;
int ans = INT_MIN;
for(int i = 1 ; i < n ; ++i){
if(nums[i] > nums[i-1]){
lcisLength++;
}else{
ans = max(ans,lcisLength);
lcisLength = 1;
}
}
ans = max(ans,lcisLength);
return ans;
}
};