forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
107 lines (83 loc) · 2.26 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// Source : https://leetcode.com/problems/max-stack/description/
/// Author : liuyubobobo
/// Time : 2017-11-27
#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 peekMax and popMax operations, which named maxStack
/// maxStack will store the current max value in the stack
/// for popMax, we will find the max value in the stack in O(n) time
///
/// Time Complexity: push: O(1)
/// pop: O(1)
/// top: O(1)
/// peekMax: O(1)
/// popMax: O(n)
/// Space Complexity: O(n)
class MaxStack {
private:
stack<int> normalStack;
stack<int> maxStack;
public:
/** initialize your data structure here. */
MaxStack() {
while(!normalStack.empty())
normalStack.pop();
while(!maxStack.empty())
maxStack.pop();
}
void push(int x) {
normalStack.push(x);
if(maxStack.empty())
maxStack.push(x);
else
maxStack.push(max(maxStack.top(), x));
}
int pop() {
assert(normalStack.size() > 0);
int v = normalStack.top();
normalStack.pop();
maxStack.pop();
return v;
}
int top() {
assert(normalStack.size() > 0);
return normalStack.top();
}
int peekMax() {
assert(normalStack.size() > 0);
return maxStack.top();
}
int popMax() {
assert(normalStack.size() > 0);
int maxValue = peekMax();
stack<int> tstack;
while(true){
int value = pop();
if(value == maxValue)
break;
tstack.push(value);
}
while(!tstack.empty()){
push(tstack.top());
tstack.pop();
}
return maxValue;
}
};
int main() {
MaxStack stack;
stack.push(5);
stack.push(1);
stack.push(5);
cout << stack.top() << endl; // -> 5
cout << stack.popMax() << endl; // -> 5
cout << stack.top() << endl; // -> 1
cout << stack.peekMax() << endl; // -> 5
cout << stack.pop() << endl; // -> 1
cout << stack.top() << endl; // -> 5
return 0;
}