-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path155.cpp
More file actions
36 lines (31 loc) · 743 Bytes
/
Copy path155.cpp
File metadata and controls
36 lines (31 loc) · 743 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
#include<bits/stdc++.h>
using std::vector;
class MinStack {
public:
vector<int>* stack;
vector<int>* min_stack;
/** initialize your data structure here. */
MinStack() {
stack = new vector<int>();
min_stack = new vector<int>();
min_stack->push_back(INT_MAX);
}
void push(int x) {
stack->push_back(x);
int pre_min = min_stack->back();
if (x < pre_min)
min_stack->push_back(x);
else
min_stack->push_back(pre_min);
}
void pop() {
stack->pop_back();
min_stack->pop_back();
}
int top() {
return stack->back();
}
int getMin() {
return min_stack->back();
}
};