findLengthOfLCIS- the longest continuous increasing sequence

Title:

Given an unsorted integer array, find the longest and continuously increasing subsequence, and return the length of the sequence.

Continuous incremental sequence can be determined by two subscripts l and r (l <r), if for each l <= i <r, there nums [i] <nums [i + 1], then
it sequences [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 an ascending subsequence, 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.

prompt:

0 <= nums.length <= 104
-109 <= nums[i] <= 109

Problem-solving ideas

This topic is to find out the length of the longest continuous subsequence, think as follows:
1. If it is an empty array, or the array is 1, just return the length of the array directly.
2. When the number is continuous, the minimum number is recorded as l, the maximum number is recorded as r-1, and the turning number is r. When a turning number (turning number-that is less than or equal to the previous number) is encountered, the record length is rl ( Recorded to the variable res)
3. Repeat 2 each time the turning number is encountered, if the length is greater, update the result length
4. When a continuous number is encountered, and the last number is also in the continuous sequence (this must be satisfied, the array The last number is greater than the previous number), if the length is greater, update res

Code demo

class Solution {
    
    
    public int findLengthOfLCIS(int[] nums) {
    
    
    if(nums.length==0||nums.length==1)
        return nums.length;
        int l=0,r=1,res=1;
        while (r<nums.length)
        {
    
    
            if(nums[r]>nums[r-1])
            {
    
    
                r++;
                continue;
            }
            res= (r-l > res) ? r-l : res;
            l= r;
            r++;
        }
        if(nums[nums.length-1]>nums[nums.length-2])
            res= (r-l > res) ? r-l : res;
        return res;
    }
}

running result

The info
answer was successful:
execution time: 1 ms, beating 99.83% of Java users
Memory consumption: 39.3 MB, beating 29.52% of Java users

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/113073529