Simple solution in python using stack

amazon
Tags: #<Tag:0x00007f242e88b090>

#1
    G=[-1]*len(A)
    stack=[]
    for i in range(len(A)-1,-1,-1):
        while(len(stack)>0):
            if(A[i]<stack[-1]):
                G[i]=stack[-1]
                break
            else:
                stack.pop()
        stack.append(A[i])
    stack=[]
    return G