-
Notifications
You must be signed in to change notification settings - Fork 0
/
155.cpp
56 lines (49 loc) · 888 Bytes
/
155.cpp
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
54
55
56
#include <list>
#include <iostream>
#include <climits>
using namespace std;
class MinStack {
private:
list<int> l;
int min;
public:
MinStack():min(INT_MAX){}
void push(int x) {
l.push_front(x);
if(x < min){
min = x;
}
}
void pop() {
int t = top();
l.pop_front();
if(t==min){
min = INT_MAX;
list<int>::iterator it = l.begin();
for(;it!=l.end();it++){
if(*it < min){
min = *it;
}
}
}
}
int top() {
return l.front();
}
int getMin() {
return min;
}
};
int main(){
MinStack m;
m.push(2);
m.push(0);
m.push(3);
m.push(0);
m.pop();
m.pop();
cout << m.getMin()<< endl;
m.pop();
cout << m.getMin()<< endl;
return 0;
}