Easy O(n) Code C++


#1

int Solution::perfectPeak(vector &A) {
int n=A.size();
vector lm(n),rm(n);
lm[0]=A[0],rm[n-1]=A[n-1];
for(int i=1;i<n-1;i++){
lm[i]=max(lm[i-1],A[i]);
rm[n-i-1]=min(rm[n-i],A[n-i-1]);
}
for(int i=1;i<n-1;i++){
if(A[i]<rm[i+1] and A[i]>lm[i-1]) return 1;
}
return 0;
}