Stack containing the Min function

Source: Internet
Author: User
Tags stack pop

Source: Niu Ke net, "Jian refers to offer"

Define the data structure of the stack, implement a min function that can get the smallest element of the stack in the type. The complexity of the Require push pop min operation is O (1).

Consider using two stacks, one data stack, equivalent to the normal stack, to implement push pop peek, and another auxiliary stack implementation of the Min function.

The key to this question is what values are stored with the secondary stack. To ensure that the top of the secondary stack is the minimum, the top of the pop is still the minimum value. That is, the secondary stack is stored from top to bottom should be the minimum value, the secondary small value, the secondary small value ...

Here is easy to enter a misunderstanding is: is the auxiliary stack is the sort of data stack? If this is the case, push when the order can not meet O (1), data stack pop, the secondary stack to find the data stack pop out of the value and then pop, also do not meet O (1).

When two stacks are empty, the first value of push in is the minimum value;

Push the second element, if the value of push < auxiliary stack top element (here is the first value), this value is pressed into the secondary stack, if the value of push is greater than the auxiliary stack top element, then push the auxiliary stack top element again.

Pop, the data stack auxiliary stack pops up the top element.

Java code:

1 ImportJava.util.Stack;2 3  Public classTT {4Stack<integer> Stack1 =NewStack<integer>();//Data Stack5Stack<integer> Stack2 =NewStack<integer>();//auxiliary stack for returning min value6 7 8      Public voidPushintnode) {9 Stack1.push (node);Ten         if(Stack2.empty ()) { One Stack2.push (node); A}Else { -             if(Node <Stack2.peek (). Intvalue ()) - Stack2.push (node); the             ElseStack2.push (Stack2.peek ()); -         } -  -     } +  -      Public voidpop () { +         if(!stack1.empty ()) A Stack1.pop (); at         if(!stack2.empty ()) - Stack2.pop (); -     } -  -      Public intTop () { -         if(!stack1.empty ()) in             returnStack1.peek (). Intvalue (); -         Else return0; to     } +  -      Public intmin () { the         if(!stack2.empty ()) *             returnStack2.peek (). Intvalue (); $         Else return0;Panax Notoginseng     } -}

Reference: "Sword point offer"

Stack containing the Min function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.