This is different from classic LIS problem. Here I want number of contiguous longest increasing subsequences!
For example:
INPUT: arr[] = { 3, 6, 10, 8, 11, 17, 16, 100 }
OUTPUT: 2
Explanation: There are 2 longest contiguous subsequences: { 3, 6, 10 }
and { 8, 11, 17 }
.
Here is a function that I have made to achieve the task. But is there any optimized way instead of this brute force approach?
#define ll long long int
ll longestIncSeq(vector<ll> temp, ll n)
{
ll count=0,mymax = -1,flag=0,dummy=0;
while( (flag==0) && (!(temp.empty())) )
{
ll max = 1, len = 1, maxIndex = 0;
for (ll i=1; i<n; i++)
{
if (temp[i] > temp[i-1])
len++;
else
{
if (max < len)
{
max = len;
maxIndex = i - max;
}
len = 1;
}
}
if (max < len)
{
max = len;
maxIndex = n - max;
}
if( max == 1 )
{
return count;
}
if( (mymax == max) || (dummy==0) )
{
count++;
dummy++;
}
else
{
flag = 1;
}
temp.erase(temp.begin()+maxIndex , temp.begin()+maxIndex+max);
mymax = max;
n = temp.size();
}
return count;
}