Esy c++ sol withou using stacks with comments


#1

vector Solution::prevSmaller(vector &A) {
int i,j;vector g(A.size());g[0]=-1;//initialising first element as zero
for(i=1;i<A.size();i++)
{
if(A[i]>A[i-1])
g[i]=A[i-1];
else. //compare with min of prev elements
{
j=i-1;
while(g[j]!=-1)
{
if(g[j]<A[i])
{
g[i]=g[j];
break;
}
j–;
}
if(g[j]==-1)
g[i]=-1;
}
}
return g;
}