674. Longest Continuous Increasing Subsequence

题目

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

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. 

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1. 

Note: Length of the array will not exceed 10,000.

思路

本题是个典型的动态规划求解;要求递增子序列的最大长度,那么每次求解以索引index为结尾的递增序列的长度maxendinghere。再维护一个maxsofar来存储到目前为止最大的长度。本题是编程珠玑第八章算法设计中提到的扫描算法,时间复杂度为O(N)

代码

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int maxendinghere = 1;
        int maxsofar = 1;
        if(nums.size()==0)
            return 0;
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]>nums[i-1])
                maxendinghere++;
            else
                maxendinghere = 1;
            maxsofar = max(maxsofar,maxendinghere);
        }
        return maxsofar;
    }
};

猜你喜欢

转载自blog.csdn.net/u010665216/article/details/79620515