Skip to content

Commit cb6090c

Browse files
authored
Create 155.min-stack.cpp
1 parent b20c1da commit cb6090c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

LeetCode/155.min-stack.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class MinStack {
2+
public:
3+
/** initialize your data structure here. */
4+
vector<int> a, b;
5+
MinStack() {
6+
a = b = {};
7+
}
8+
9+
void push(int x) {
10+
a.push_back(x);
11+
if(b.empty()) b.push_back(x);
12+
else {
13+
int newMin = min(x, *b.rbegin());
14+
b.push_back(newMin);
15+
}
16+
}
17+
18+
void pop() {
19+
a.pop_back();
20+
b.pop_back();
21+
}
22+
23+
int top() {
24+
return *a.rbegin();
25+
}
26+
27+
int getMin() {
28+
return *b.rbegin();
29+
}
30+
};
31+
32+
/**
33+
* Your MinStack object will be instantiated and called as such:
34+
* MinStack* obj = new MinStack();
35+
* obj->push(x);
36+
* obj->pop();
37+
* int param_3 = obj->top();
38+
* int param_4 = obj->getMin();
39+
*/

0 commit comments

Comments
 (0)