← Home

155. Min Stack

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
155

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 155. Min Stack, the solution in this repository is mainly a data structure design solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design, stack.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are push, pop, top, getMin.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 240 ms, faster than 5.05% of C++ online submissions for Min Stack.
02//Memory Usage: 16.7 MB, less than 97.85% of C++ online submissions for Min Stack.
03
04class MinStack {
05public:
06    /** initialize your data structure here. */
07    vector<int> v;
08    MinStack() {
09        vector<int> v;
10    }
11    
12    void push(int x) {
13        v.push_back(x);
14    }
15    
16    void pop() {
17        v.erase(v.begin()+v.size()-1);
18    }
19    
20    int top() {
21        //return v[v.size()-1];
22        return v.back();
23    }
24    
25    int getMin() {
26        return *min_element(v.begin(), v.end());
27    }
28};
29
30/**
31 * Your MinStack object will be instantiated and called as such:
32 * MinStack* obj = new MinStack();
33 * obj->push(x);
34 * obj->pop();
35 * int param_3 = obj->top();
36 * int param_4 = obj->getMin();
37 */
38 
39//https://leetcode.com/problems/min-stack/discuss/49014/Java-accepted-solution-using-one-stack
40//Runtime: 36 ms, faster than 84.44% of C++ online submissions for Min Stack.
41//Memory Usage: 16.7 MB, less than 99.73% of C++ online submissions for Min Stack.
42
43class MinStack {
44public:
45    /** initialize your data structure here. */
46    stack<int> stk;
47    int m = INT_MAX;
48    MinStack() {
49    }
50    
51    void push(int x) {
52        if(x <= m){ //note it's <=, not <!
53            //2nd smallest value is under 1st smallest value
54            stk.push(m);
55            m = x;
56        }
57        stk.push(x);
58    }
59    
60    void pop() {
61        int tmp = stk.top();
62        stk.pop();
63        //pop 2nd smallest value along 1st smallest value
64        if(tmp == m){
65            //when pop 1st smallest value
66            //update m to 2nd smallest value
67            m = stk.top();
68            stk.pop();
69        }
70    }
71    
72    int top() {
73        return stk.top();
74    }
75    
76    int getMin() {
77        return m;
78    }
79};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.