C++ solution O(n) but TLE! HELP. WHAT IS WRONG IN MY CODE?


#1

int Solution::perfectPeak(vector &A) {
int ans;
int flag=0;
for(int i=1;i<A.size()-1;i++)
{
int maxi=*max_element(A.begin(),A.begin()+i);
int mini=*min_element(A.begin()+i+1,A.end());
if(A[i]>maxi && A[i]<mini)
{
flag=1;
break;
}
}
return flag;

}


#2

*max_element and *min_element has time complexity O(n). Hence, your code has actually O(n^2) complexity.