-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path859.max-stack.cpp
More file actions
184 lines (165 loc) · 3.36 KB
/
859.max-stack.cpp
File metadata and controls
184 lines (165 loc) · 3.36 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Tag: Stack
// Time: -
// Space: O(N)
// Ref: Leetcode-716
// Note: -
// Design a max stack that supports push, pop, top, peekMax and popMax.
//
// 1.
// push(x) -- Push element x onto stack.
// 2.
// pop() -- Remove the element on top of the stack and return it.
// 3.
// top() -- Get the element on the top.
// 4.
// peekMax() -- Retrieve the maximum element in the stack.
// 5.
// popMax() -- Retrieve the maximum element in the stack, and remove it.
// If you find more than one maximum elements, only remove the top-most one.
//
// ---
// *
//
// ```
// Input:
// push(5)
// push(1)
// push(5)
// top()
// popMax()
// top()
// peekMax()
// pop()
// top()
// Output:
// 5
// 5
// 1
// 5
// 1
// 5
// ```
//
// 1. `-1e7 <= x <= 1e7`
// 2. Number of operations won't exceed `10000`.
// 3. The last four operations won't be called when stack is empty.
class MaxStack {
public:
stack<int> st;
stack<int> max_st;
MaxStack() {
// do intialization if necessary
}
/*
* @param number: An integer
* @return: nothing
*/
void push(int number) {
// write your code here
int max_val = max_st.empty() ? number : max_st.top();
st.push(number);
max_st.push(max_val > number ? max_val : number);
}
/*
* @return: An integer
*/
int pop() {
// write your code here
int val = top();
st.pop();
max_st.pop();
return val;
}
/*
* @return: An integer
*/
int top() {
// write your code here
return st.top();
}
/*
* @return: An integer
*/
int peekMax() {
// write your code here
return max_st.top();
}
/*
* @return: An integer
*/
int popMax() {
// write your code here
int max_val = peekMax();
stack<int> tmp;
while (top() != max_val) {
tmp.push(top());
pop();
}
pop();
while (!tmp.empty()) {
push(tmp.top());
tmp.pop();
}
return max_val;
}
};
#include <list>
class MaxStack {
public:
list<int> st;
map<int, vector<list<int>::iterator>> hash;
MaxStack() {
// do intialization if necessary
}
/*
* @param number: An integer
* @return: nothing
*/
void push(int number) {
// write your code here
st.push_back(number);
hash[number].push_back(prev(st.end()));
}
/*
* @return: An integer
*/
int pop() {
// write your code here
int val = st.back();
st.pop_back();
hash[val].pop_back();
if (hash[val].empty()) {
hash.erase(val);
}
return val;
}
/*
* @return: An integer
*/
int top() {
// write your code here
return st.back();
}
/*
* @return: An integer
*/
int peekMax() {
// write your code here
return prev(hash.end())->first;
}
/*
* @return: An integer
*/
int popMax() {
// write your code here
auto it = prev(hash.end());
int val = it->first;
auto last = it->second.back();
st.erase(last);
it->second.pop_back();
if (it->second.empty()) {
hash.erase(it);
}
return val;
}
};