Skip to content

Commit cd1bb24

Browse files
[CREATE] [CPP] 155. Min Stack
1 parent dc31034 commit cd1bb24

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

cpp/155-Min-Stack.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using namespace std;
2+
class MinStack {
3+
public:
4+
stack<pair<int, int>> stk;
5+
int m;
6+
7+
MinStack() {
8+
m = INT_MAX;
9+
}
10+
11+
void push(int val) {
12+
stk.push({val, min(val, stk.empty()?INT_MAX:stk.top().second)});
13+
}
14+
15+
void pop() {
16+
if(!stk.empty()) stk.pop();
17+
}
18+
19+
int top() {
20+
return stk.top().first;
21+
}
22+
23+
int getMin() {
24+
return stk.top().second;
25+
}
26+
};
27+
28+
/**
29+
* Your MinStack object will be instantiated and called as such:
30+
* MinStack* obj = new MinStack();
31+
* obj->push(val);
32+
* obj->pop();
33+
* int param_3 = obj->top();
34+
* int param_4 = obj->getMin();
35+
*/

0 commit comments

Comments
 (0)