O(n) space and O(n) time complexity C++ solution


#1

int Solution::perfectPeak(vector &A)
{
int i;
int n=A.size();
//cout<<‘a’;
int small[A.size()];
small[A.size()-1]=A[A.size()-1];
//cout<<small[A.size()-1];
for(i=A.size()-2;i>=0;i–)
{
small[i]=min(small[i+1],A[i]);
//cout<<small[i];
}
int max=A[0];
for(i=1;i<A.size()-1;i++)
{
if(A[i]>max && A[i]<small[i+1])
{
max=A[i];
return 1;
}
if(A[i]>max)
{
max=A[i];
}
}
return 0;

}