</>

155. Min Stack

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Stack
Time
—
Space
—

Standard Solution (C++)

SummaryDesign a stack that supports push, pop, top, and retrieval of the current minimum element, all in O(1) time.

Approach
  • Maintain a primary stack for all values and a secondary "min-tracker" stack whose top always reflects the current minimum.
  • On push, always add to the primary stack; add to the min-tracker only when the new value is less than or equal to the current minimum (handles duplicates correctly).
  • On pop, remove from the primary stack; also pop the min-tracker when its top matches the removed value, keeping the two stacks in sync.
  • getMin is always O(1) because the min-tracker's top is guaranteed to equal the smallest value currently in the primary stack.
#include <stack>
using namespace std;

class MinStack {
    stack<int> main_;   // holds every pushed value
    stack<int> mins_;   // top is always the current minimum

public:
    MinStack() {}

    void push(int val) {
        main_.push(val);
        // Push onto the min-tracker when empty or val is a new (or tied) minimum.
        // Tracking equal values ensures correctness when the current minimum is
        // popped while a duplicate of it still exists in the stack.
        if (mins_.empty() || val <= mins_.top()) {
            mins_.push(val);
        }
    }

    void pop() {
        // If the element being removed is the current minimum, retire it from
        // the min-tracker so the next-smallest value surfaces at the top.
        if (main_.top() == mins_.top()) {
            mins_.pop();
        }
        main_.pop();
    }

    int top() {
        return main_.top();
    }

    int getMin() {
        return mins_.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */
Time O(1) for all operations (push, pop, top, getMin) Space O(n) for the two stacks, where n is the number of elements currently in the MinStack

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <stack>
using namespace std;

class MinStack {
public:
    stack<int> s1;
    stack<int> s2;
    MinStack() {}
    
    void push(int val) {
        s1.push(val);
        if(s2.empty() || s2.top()>=val){
            s2.push(val);
        }
    }
    
    void pop() {
        if(s2.top()==s1.top()){
            s2.pop();
        }
        s1.pop();
    }
    
    int top() {
        return s1.top();
    }
    
    int getMin() {
        return s2.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

// Time: O(1) for all operations
// Space: O(n) for the two stacks, where n is the number of elements in the MinStack

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground C++

Your solution. A driver couldn't be auto-generated for this signature — add a main() that calls the method and prints the result, then Run.

C++ ▶ Runnable Couldn't auto-generate a driver for this signature — edit to add one.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern