Easy stack approach in C++


#1

vector Solution::prevSmaller(vector &A)
{
stackstk;
int i;
vectorv(A.size());
stk.push(0);
v[0]=-1;
for(i=1;i<A.size();i++)
{
while(!stk.empty() && A[i]<=A[stk.top()])
{
stk.pop();
}
if(stk.empty())
{
v[i]=-1;
}
else
{
v[i]=A[stk.top()];
}
stk.push(i);
}
return v;
}