-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155.cpp
More file actions
67 lines (54 loc) · 1.29 KB
/
155.cpp
File metadata and controls
67 lines (54 loc) · 1.29 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <stack>
using namespace std;
class MinStack {
public:
MinStack() {
}
void push(int val) {
if (min_stk.empty() || val < min_stk.top()) {
min_stk.push(val);
}
stk.push(val);
}
void pop() {
if (stk.top() == min_stk.top()) {
min_stk.pop();
}
stk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return min_stk.top();
}
private:
stack<int> stk;
stack<int> min_stk;
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
int main() {
// ["MinStack","push","push","push","getMin","top","pop","getMin"]
// [[],[-2],[0],[-1],[],[],[],[]]
// [null,null,null,null,-2,-1,null,-2]
MinStack* obj = new MinStack();
obj->push(-2);
obj->push(0);
obj->push(-1);
std::cout << "Min: " << obj->getMin() << std::endl; // Should print -2
std::cout << "Top: " << obj->top() << std::endl; // Should print -1
obj->pop();
std::cout << "Min after pop: " << obj->getMin()
<< std::endl; // Should print -2
// Clean up
delete obj;
return 0;
}