-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path155. Min Stack.cpp
More file actions
34 lines (29 loc) · 659 Bytes
/
155. Min Stack.cpp
File metadata and controls
34 lines (29 loc) · 659 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
class MinStack {
public:
vector<pair<int,int>> st; //we have make a vector
MinStack() {
}
void push(int val) {
if(st.empty()){
pair<int,int>p;// = make_pair(val,val);
p.first = val;
p.second = val;
st.push_back(p);
}
else{
pair<int,int>p;
p.first = val;
p.second = min(val, st.back().second);
st.push_back(p);
}
}
void pop() {
st.pop_back();
}
int top() {
return st.back().first;
}
int getMin() {
return st.back().second;
}
};