Using Stack | O(n)


#1

Comment body goevector Solution::prevSmaller(vector &A) {
int n=A.size();
stacks;
vectorleft(n);
left[0]=-1;
s.push(0);
for(int i=1;i<n;i++){
while(!s.empty() && A[i]<=A[s.top()])s.pop();
if(s.empty())left[i]=-1;
else left[i]=A[s.top()];
s.push(i);
}
return left;
}