Skip to content

Commit

Permalink
Time: 15 ms (77.22%), Space: 22.2 MB (50.69%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakkumar9546 committed Aug 31, 2024
1 parent 382f95b commit db58a10
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 0155-min-stack/0155-min-stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class MinStack {
vector<vector<int>> st;

public:
MinStack() {}

void push(int val) {
int min_val = getMin();
if (st.empty() || min_val > val) {
min_val = val;
}
st.push_back({val, min_val});
}

void pop() { st.pop_back(); }

int top() { return st.empty() ? -1 : st.back()[0]; }

int getMin() { return st.empty() ? -1 : st.back()[1]; }
};

/**
* 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();
*/

0 comments on commit db58a10

Please sign in to comment.