Likou 674. The longest continuous increasing sequence

674. Longest Continuous Increasing Sequence

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 the 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.

示例 1:

输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。 
示例 2:

输入:nums = [2,2,2,2,2]
输出:1
解释:最长连续递增序列是 [2], 长度为1。
 

提示:

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

answer:

Just loop, because we just want the length, so there is no need to open up space to store the longest continuous increasing sequence.

Code

int Max(int x,int y)
{
    
    
    return x>y?x:y;
}
int findLengthOfLCIS(int* nums, int numsSize){
    
    
    if(numsSize==0)
        return 0;
    int length = 1;
    int max = INT_MIN;;
    for(int i=1;i<numsSize;i++)
    {
    
    
        if(nums[i]>nums[i-1])
        {
    
    
            length++;
        }
        else
        {
    
    
            max = Max(length,max);
            length = 1;
        }
    }
    return max>length?max:length;//因为可能一for到底了,每经过max的选择
}

Guess you like

Origin blog.csdn.net/xiangguang_fight/article/details/115023937