C++ Solution with Priority Queue, 42 ms, Beat 97.8%


  • 0
    T

    class MinStack {
    public:
    stack<int> myStack;
    priority_queue<int, vector<int>, std::greater<int>> myQueue;
    /** initialize your data structure here. */
    MinStack() {}

    void push(int x) {
       myStack.push(x);
       myQueue.push(x);
    }
    
    void pop() {
       if(myStack.top() == myQueue.top())
            myQueue.pop();
       myStack.pop();
    }
    
    int top() {
       return myStack.top();
    }
    
    int getMin() {
       return myQueue.top();
    }
    

    };


  • 0
    Y

    Test following input:

    ["MinStack","push","push","push","push","push","push","pop","pop","pop","getMin"]
    [[],[7],[8],[9],[-3],[1],[2],[],[],[],[]]

    The expected out put is 7 when first call getMin(), but your code output 1 instead.Wrong code but still pass the judge.The test cases are not strong enough for this problem!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.