@SAMM : Nice way to keep the track of the smallest number. But by your way we won't be able to do search in constant time. @Don : I was asked this question during an interview, so I think there must/might be some possible solution.
On Tue, Mar 6, 2012 at 3:55 AM, SAMM <[email protected]> wrote: > Use two stack :- > One stack is used for inserting and deleting the elements in the stack , > supposing the the addition and deletion is done at the end of the stack . > So it will be of in constant time . > > The Second Stack is used keeping track of the smallest number . > > For finding the element we need to used Separate Channing in hashing . > > Take the element to be added suppose (N). > Now compare the top element of second stack (suppose X) with N . > > If (N < X) Added N to both the Stack . > else if (N > X) Added N to the first Stack . > > When the element(N) is removed from the top of the first stack. Then > compare it with the second stack (X) . > If (N = X) Remove N from both the Stack . > > > The Second stack will ensure that on removing or adding an element in the > stack , The second stack will let u know the smallest element . > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
