题目描述:
主要思路:
利用辅助栈来实现,一个最小栈用类似于单调栈的方法进行维护,具体见代码。
class MinStack {
private:
stack<int> x;
stack<int> minx;
public:
MinStack() {
minx.push(INT_MAX);
}
void push(int val) {
x.push(val);
minx.push(min(minx.top(),val));
}
void pop() {
x.pop();
minx.pop();
}
int top() {
return x.top();
}
int getMin() {
return minx.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/