-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00155-min_stack.cpp
More file actions
53 lines (40 loc) · 821 Bytes
/
00155-min_stack.cpp
File metadata and controls
53 lines (40 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 155: Min stack
// https://leetcode.com/problems/min-stack/
#include<iostream>
#include<stack>
using namespace std;
// SOLUTION
class MinStack {
private:
stack<int> s1, s2;
public:
void push(int x) {
s1.push(x);
if (s2.empty() || x <= getMin())
s2.push(x);
}
void pop() {
if (s1.top() == getMin())
s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
};
int main() {
MinStack *o = new MinStack();
// OPERATIONS
o->push(-2);
o->push(0);
o->push(-3);
cout<<o->getMin()<<" ";
o->pop();
o->top();
cout<<o->getMin()<<" ";
cout<<endl;
return 0;
}