Skip to content

Commit 3cb46cf

Browse files
committed
Time: 20 ms (88.48%), Space: 16.4 MB (67.24%) - LeetHub
1 parent a8e3209 commit 3cb46cf

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

155-min-stack/155-min-stack.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class MinStack {
2+
public:
3+
stack<int> s;
4+
stack<int> ss;
5+
MinStack() {
6+
7+
}
8+
9+
void push(int val) {
10+
s.push(val);
11+
if(ss.empty() || ss.top() >= val) {
12+
ss.push(val);
13+
}
14+
}
15+
16+
void pop() {
17+
int pop_value = s.top();
18+
s.pop();
19+
if(ss.top() == pop_value){
20+
ss.pop();
21+
}
22+
}
23+
24+
int top() {
25+
return s.top();
26+
}
27+
28+
int getMin() {
29+
return ss.top();
30+
}
31+
};
32+
33+
/**
34+
* Your MinStack object will be instantiated and called as such:
35+
* MinStack* obj = new MinStack();
36+
* obj->push(val);
37+
* obj->pop();
38+
* int param_3 = obj->top();
39+
* int param_4 = obj->getMin();
40+
*/

0 commit comments

Comments
 (0)