Design a max stack that supports push, pop, top, peekMax and popMax.
push(x) – Push element x onto stack.
pop() – Remove the element on top of the stack and return it.
top() – Get the element on the top.
peekMax() – Retrieve the maximum element in the stack.
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.
Example 1:
MaxStack stack = new MaxStack();
stack.push(5);
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5
Note:
- -1e7 <= x <= 1e7
- Number of operations won’t exceed 10000.
- The last four operations won’t be called when stack is empty.
方法1: two stacks
思路:
参照155. Min Stacks,再加一个popMax的功能。popMax时用一个buffer来缓存从st中寻找最大值时暂时倒出来的数字,同时helper对应数字也被pop出来。找到后,两个栈同时pop掉最大值,调用push将buffer中的数字再存回去。注意清空和放回要用MaxStack的api才能保证helper被同时更新。
Complexity
Time complexity: 1 ~ 4, O(1), 5, O(n)
Space complexity: O(n)
class MaxStack {
private:
stack<int> st, helper;
public:
/** initialize your data structure here. */
MaxStack() {
}
void push(int x) {
if (!helper.empty()) {
x > helper.top() ? helper.push(x) : helper.push(helper.top());
} else {
helper.push(x);
}
st.push(x);
}
int pop() {
int val = st.top();
st.pop();
helper.pop();
return val;
}
int top() {
return st.top();
}
int peekMax() {
return helper.top();
}
int popMax() {
int val = helper.top();
stack<int> buffer;
while (st.top() != val) {
buffer.push(st.top());
st.pop();
helper.pop();
}
st.pop();
helper.pop();
while (!buffer.empty()) {
push(buffer.top());
buffer.pop();
}
return val;
}
};
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack* obj = new MaxStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->peekMax();
* int param_5 = obj->popMax();
*/
方法2: doubly linked list + treemap
官方题解:https://leetcode.com/problems/max-stack/solution/
思路:
为了使popMax的时间降到O(n)以下,用doubly linked list 来保存数字,类似于之前的stack,但是可以做到O(1) 时间删除某一个节点。但是要怎么知道最大值保存在哪里呢?这就要借助一个辅助map来保存每一个val所对应在dll中的所有iterator,用val作为key,用一个vector来作为hash的val。而为了随时可以找到val中的最大值,需要用treemap来对这些key进行排序。这样在删除时(pop, poMax),可以找到要删除的val,然后在其对应的vector 中找到最后一个值,用这个iterator在dll中用O(1)完成删除,再将iterator本身从hash中pop掉。还要注意如果hash[val]此时为空的话, 需要将val从hash中删除,不要误导下面的getMax。
Complexity
Time complexity: O(log n)
Space complexity: O(n)
class MaxStack {
private:
list<int> dll;
map<int, vector<list<int>::iterator>> hash;
public:
/** initialize your data structure here. */
MaxStack() {
}
void push(int x) {
dll.push_front(x);
hash[x].push_back(dll.begin());
}
int pop() {
int val = dll.front();
hash[val].pop_back();
dll.pop_front();
if (hash[val].empty()) hash.erase(val);
return val;
}
int top() {
return dll.front();
}
int peekMax() {
return hash.rbegin()->first;
}
int popMax() {
int val = hash.rbegin()->first;
auto it = hash[val].back();
dll.erase(it);
hash[val].pop_back();
if (hash[val].empty()) hash.erase(val);
return val;
}
};
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack* obj = new MaxStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->peekMax();
* int param_5 = obj->popMax();
*/