Nearest Smaller to Left
Problem Link: https://www.interviewbit.com/problems/nearest-smaller-element/
class Solution:
# @param arr : list of integers
# @return a list of integers
def prevSmaller(self, nums):
s=[]
n=len(nums)
ans=[]
for i in range(n):
if(len(s)==0):
ans.append(-1)
else:
if(s[-1]<nums[i]):
ans.append(s[-1])
else:
while(len(s)>0 and s[-1]>=nums[i]):
s.pop()
if(len(s)==0):
ans.append(-1)
else:
ans.append(s[-1])
s.append(nums[i])
return ans
Last updated