[Programmer interview question selection 100 questions] 2. Design a stack containing min functions, programmer min

Source: Internet
Author: User

[Programmer interview question selection 100 questions] 2. Design a stack containing min functions, programmer min
[Question]

Define the data structure of the stack. Add a min function to obtain the minimum element of the stack. The time complexity of the min, push, and pop functions is O (1 ).

[Analysis]

This is an interview question from google last year.

When I see this question, the first reaction is to sort all reverse elements in the stack every time a new element is pushed. In this way, the top element of the stack will be the smallest element. However, the data structure designed in this way is no longer a stack because it cannot guarantee that the elements pushed to the stack are first-in-first-out.

Add a member variable to the stack to store the smallest element (or the location of the smallest element ). Each time a new element is pushed to the stack, if the element is smaller than the current minimum element, the minimum element is updated.

At first glance, this is a good idea. But if you think about it, there is an important question: if the current minimum element is pop out, how can we get the next minimum element?

Therefore, it is not enough to add only one member variable to store the smallest element (or the location of the smallest element. We need an auxiliary stack. Each time a new element is pushed, the smallest element (or the position of the smallest element) is also pushed. Considering that the type of the stack element may be a complex data structure, using the location of the smallest element can reduce space consumption) push to the auxiliary stack; each time an element pops out of the stack, pop auxiliary stack at the same time.

[Code]

/********************************** Date: * Author: SJF0115 * Title: Stack containing min functions * Source: Google * category: classic interview questions *********************************/# include <iostream> # include <vector> # include <assert. h> using namespace std; template <typename T> class MinStack {private: // The element vector in the stack <T> vals; // The current smallest element vector <T> minVals; public: // push (T val) into the stack void; // T top () on the top of the stack element; // return the stack void pop (); // whether the stack is empty bool empty (); // minimum T min () ;}; // stack entry template <typename T> void MinStack <T>: push (T val) {// Add the element to the stack vals. push_back (val); // The auxiliary stack is empty. The element in the stack is the minimum value if (minVals. size () = 0) {minVals. push_back (val);} // if // The secondary stack is not empty. Compare the element of the stack to the current minimum element. else {// update the smallest element of the secondary stack if (val <minVals. front () {minVals. push_back (val);} else {minVals. push_back (minVals. front ();} // if} // whether the stack is empty template <typename T> bool MinStack <T >:: empty () {if (vals. empty () {return true;} else {return false ;}// template on the top of the stack <typename T> T MinStack <T >:: top () {assert (vals. size ()> 0); return vals. back () ;}// out-of-stack template <typename T> void MinStack <T >:: pop () {if (empty ()) {cout <"Empty stack with no element to exit stack" <endl;} else {vals. pop_back (); minVals. pop_back () ;}}// the minimum template <typename T> T MinStack <T>: min () {assert (vals. size ()> 0); return minVals. back () ;}int main () {MinStack <int> minStack; // minStack. top (); minStack. push (3); cout <"Min:" <minStack. min () <endl; minStack. push (4); cout <"Min:" <minStack. min () <endl; minStack. push (2); cout <"Min:" <minStack. min () <endl; minStack. push (1); cout <"Min:" <minStack. min () <endl; minStack. pop (); cout <"Min:" <minStack. min () <endl; cout <"Top:" <minStack. top () <endl; return 0 ;}


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.