forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
80 lines (63 loc) · 1.65 KB
/
main2.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/// Source : https://leetcode.com/problems/max-stack/description/
/// Author : liuyubobobo
/// Time : 2017-11-28
#include <iostream>
#include <stack>
#include <cassert>
using namespace std;
/// Using two stacks
/// We have one regular stack for push, pop and top operations
/// We have another stack for getMin operations, which named minStack
/// minStack will store the current min value in the stack
///
/// Time Complexity: push: O(1)
/// pop: O(1)
/// top: O(1)
/// getMin: O(1)
/// Space Complexity: O(n)
class MinStack {
private:
stack<int> normalStack;
stack<int> minStack;
public:
/** initialize your data structure here. */
MinStack() {
while(!normalStack.empty())
normalStack.pop();
while(!minStack.empty())
minStack.pop();
}
void push(int x) {
normalStack.push(x);
if(minStack.empty())
minStack.push(x);
else
minStack.push(min(minStack.top(), x));
}
int pop() {
assert(normalStack.size() > 0);
int v = normalStack.top();
normalStack.pop();
minStack.pop();
return v;
}
int top() {
assert(normalStack.size() > 0);
return normalStack.top();
}
int getMin() {
assert(normalStack.size() > 0);
return minStack.top();
}
};
int main() {
MinStack minStack;
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
cout << minStack.getMin() << endl; // -> -3
minStack.pop();
cout << minStack.top() << endl; // -> 0
cout << minStack.getMin() << endl; // -> -2
return 0;
}