-
Notifications
You must be signed in to change notification settings - Fork 1
/
155.cpp
52 lines (40 loc) · 868 Bytes
/
155.cpp
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
// 155. Min Stack - https://leetcode.com/problems/min-stack
#include <bits/stdc++.h>
using namespace std;
class MinStack {
private:
stack<int> min_st;
stack<int> st;
public:
MinStack() {}
void push(int x) {
st.push(x);
if (min_st.empty() || min_st.top() > x) {
min_st.push(x);
} else {
min_st.push(min_st.top());
}
}
void pop() {
st.pop();
min_st.pop();
}
int top() {
return st.top();
}
int getMin() {
return min_st.top();
}
};
int main() {
ios::sync_with_stdio(false);
MinStack minStack;
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
assert(minStack.getMin() == -3);
minStack.pop();
assert(minStack.top() == 0);
assert(minStack.getMin() == -2);
return 0;
}