We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b20c1da commit cb6090cCopy full SHA for cb6090c
LeetCode/155.min-stack.cpp
@@ -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