A simple solution using stack


#1

vector Solution::prevSmaller(vector &A) {
vector G;
stack s;

for(int i=0;i<A.size();i++){
    while(s.empty()==false && A[i]<=s.top()){
        s.pop();
    }
    if(s.empty()==true) G.push_back(-1);
    else G.push_back(s.top());
    s.push(A[i]);
}

return G;

}