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.
A 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 < r
, nums[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 = 0; i < nums.size() - 1; i++){if (nums[i + 1] - nums[i] > 0)currentCount++;else{prevCount = max(prevCount, currentCount);currentCount = 0;}}return max(prevCount, currentCount) + 1;}int main(){vector<int> nums = {1, 3, 5, 4, 7};cout << findLengthOfLCIS(nums);return 0;}
No comments:
Post a Comment