Longest Continuous Increasing Subsequence

Given an unsorted array of integer nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.

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 < rnums[i] < nums[i + 1].

Example:

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.

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

int findLengthOfLCIS(vector<int&nums)
{
    int prevCount = INT_MIN;
    int currentCount = 0;
    if (nums.size() == 0)
        return 0;
    for (int i = 0i < nums.size() - 1i++)
    {
        if (nums[i + 1] - nums[i] > 0)
            currentCount++;
        else
        {
            prevCount = max(prevCountcurrentCount);
            currentCount = 0;
        }
    }
    return max(prevCountcurrentCount) + 1;
}

int main()
{
    vector<intnums = {13547};

    cout << findLengthOfLCIS(nums);

    return 0;
}


No comments:

Post a Comment