class Solution {
public:
stack<int> st ,st_min;//由于新加的最小值只与当前的最小值有关,所以可以开一个新栈表示当前最小值
// 车位 4 6 2 1 6 3 对应最小值 1 1 1 1 3 3 这样移车时只需将最小值移除而不用重新计算
void push(int value) {//
st.push(value);
if(!st_min.empty()){
int ai = st_min.top();
if(ai > value){
st_min.push(value);
}else
st_min.push(ai);
}else st_min.push(value);
}
void pop() {
if (!st.empty()){
st.pop();
st_min.pop();
}
}
int top() {
int ai = st.top();
return ai;
}
int min() {
return st_min.top();
}
};

京公网安备 11010502036488号