Fork me on GitHub

设计一个有getMin功能的栈

题目

实现一个特殊的栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作。

要求:

  1. pop、push、getMin操作的时间复杂度都是O(1)
  2. 设计的栈类型可以使用现成的栈结构。

代码实现

时间复杂度O(1),空间复杂度O(n)

这也是书上的写法,使用两个栈,一个正常存数据,一个维护当前栈中最小元素。

#include <bits/stdc++.h>
using namespace std;

struct MyStack{
    stack<int> stackData;
    stack<int> stackMin;
    void getMin(){
        if(!stackMin.empty()){
            cout<<"Minimum element in stack is "<<stackMin.top()<<endl;
        }
    }
    void pop(){
        if(stackData.empty()){
            cout<<"Stack is empty"<<endl;
            return;
        }
        if(stackData.top()==stackMin.top()){
            stackMin.pop();
        }
        cout<<"Top Most Element Removed "<<stackData.top()<<endl;
        stackData.pop();
    }
    void push(int x){
        cout<<"Stack Insert Element "<<x<<endl;
        if(stackMin.empty() || x<=stackMin.top()){
            stackMin.push(x);
        }
        stackData.push(x);
    }
    void peek(){
        cout<<"Top Most Element is "<<stackMin.top()<<endl;
    }
};

int main(){
    MyStack s;
    s.push(3);
    s.push(5);
    s.getMin();
    s.push(2);
    s.push(1);
    s.getMin();
    s.pop();
    s.getMin();
    s.pop();
    s.peek();
    s.getMin();
    return 0;
}

时间复杂度O(1),空间复杂度O(1)

参考

Push(x) : Inserts x at the top of stack.

  • If stack is empty, insert x into the stack and make minEle equal to x.
  • If stack is not empty, compare x with minEle. Two cases arise:
    • If x is greater than or equal to minEle, simply insert x.
    • If x is less than minEle, insert (2x – minEle) into the stack and make minEle equal to x. For example, let previous minEle was 3. Now we want to insert 2. We update minEle as 2 and insert 22 – 3 = 1 into the stack.

Pop() : Removes an element from top of stack.

  • Remove element from top. Let the removed element be y. Two cases arise:
    • If y is greater than or equal to minEle, the minimum element in the stack is still minEle.
    • If y is less than minEle, the minimum element now becomes (2minEle – y), so update (minEle = 2minEle – y). This is where we retrieve previous minimum from current minimum and its value in stack. For example, let the element to be removed be 1 and minEle be 2. We remove 1 and update minEle as 2*2 – 1 = 3.

Important Points:

  • Stack doesn’t hold actual value of an element if it is minimum so far.
  • Actual minimum element is always stored in minEle
#include <bits/stdc++.h>
using namespace std;

struct MyStack{
    stack<int> s;
    int minEle;
    void getMin(){
        if(s.empty()){
            cout<<"Stack is empty"<<endl;
            return;
        } else{//minEle保存栈中最小元素的值
            cout<<"Mininum Element in the stack is:"<<minEle<<endl;
        }
    }
    void peek(){
        if(s.empty()){
            cout<<"Stack is empty"<<endl;
            return;
        }
        int t=s.top();
        cout<<"Top Most Element is:";
        //如果栈顶t<minEle意味着minEle保存者t的值
        (t<minEle)?cout<<minEle:cout<<t;
    }
    void pop(){
        if(s.empty()){
            cout<<"Stack is empty"<<endl;
            return;
        }
        
        cout<<"Top Most Element Removed:";
        int t=s.top();
        s.pop();

        if(t<minEle){
            cout<<minEle<<endl;
            minEle=2*minEle-t;
        }else{
            cout<<t<<endl;
        }
    }
    void push(int x){
        if(s.empty()){
            minEle=x;
            s.push(x);
            cout<<"Nember Inserted:"<<x<<endl;
            return;
        }
        if(x<minEle){
            s.push(2*x-minEle);
            minEle=x;
        }else{
            s.push(x);
        }
        cout<<"Number Inserted:"<<x<<endl;
    }
};

int main(){
    MyStack s;
    s.push(3); 
    s.push(5); 
    s.getMin(); 
    s.push(2); 
    s.push(1); 
    s.getMin(); 
    s.pop(); 
    s.getMin(); 
    s.pop(); 
    s.peek(); 
  
    return 0; 
}
posted @ 2019-11-07 23:49  克拉默与矩阵  阅读(253)  评论(0)    收藏  举报