Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created February 13, 2018 14:22
Show Gist options
  • Save s4553711/f54a889f78373903cfffb4966068883a to your computer and use it in GitHub Desktop.
Save s4553711/f54a889f78373903cfffb4966068883a to your computer and use it in GitHub Desktop.
class MinStack {
public:
stack<int> all;
stack<int> minsta;
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
if (all.empty()) {
all.push(x);
minsta.push(x);
} else {
all.push(x);
if (x <= minsta.top()) minsta.push(x);
}
}
void pop() {
if (all.top() == minsta.top()) {
minsta.pop();
}
all.pop();
}
int top() {
return all.top();
}
int getMin() {
return minsta.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment