Why is it showing TLE for O(3n) solution?

programming
Tags: #<Tag:0x00007f2428261030>

#1

code:

int Solution::perfectPeak(vector &A) {
int n=A.size();
int prevGreat[n],nextSmall[n];
prevGreat[0]=A[0];
for(int i=1;i<n;i++)
{
prevGreat[i]=max(prevGreat[i-1],A[i]);
}
nextSmall[n-1]=A[n-1];
int i;
for(i=n-2;i>=0;i–)
{
nextSmall[i]=min(nextSmall[i+1],A[i]);
}
int j;
for(j=1;j<n-1;i++)
{
if(prevGreat[j]!=prevGreat[j-1] && nextSmall[j]!=nextSmall[j+1])
return 1;
}
return 0;

}


#2

In your 3rd loop you should increment j not i.