Skip to content

Commit cc488f2

Browse files
authored
155. Min Stack (using vector)
1 parent 2412608 commit cc488f2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

155. Min Stack

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class MinStack {
2+
public:
3+
vector<pair<int,int>> st; //we have make a vector
4+
MinStack() {
5+
6+
}
7+
8+
void push(int val) {
9+
if(st.empty()){
10+
pair<int,int>p;// = make_pair(val,val);
11+
p.first = val;
12+
p.second = val;
13+
st.push_back(p);
14+
}
15+
else{
16+
pair<int,int>p;
17+
p.first = val;
18+
p.second = min(val, st.back().second);
19+
st.push_back(p);
20+
}
21+
}
22+
23+
void pop() {
24+
st.pop_back();
25+
}
26+
27+
int top() {
28+
return st.back().first;
29+
}
30+
31+
int getMin() {
32+
return st.back().second;
33+
}
34+
};

0 commit comments

Comments
 (0)