-
Notifications
You must be signed in to change notification settings - Fork 0
155_MinStack
a920604a edited this page Apr 14, 2023
·
1 revision
class MinStack {
private:
stack<int> sta, minsta;
public:
MinStack() {
}
void push(int val) {
sta.push(val);
// 單調遞減stack
if(minsta.empty()) minsta.push(val);
else if(!minsta.empty() && minsta.top()>=val ) minsta.push(val);
}
void pop() {
int t = sta.top(); sta.pop();
if(t == minsta.top()) minsta.pop();
}
int top() {
return sta.top();
}
int getMin() {
return minsta.top();
}
};
footer