Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MinStack {
- public:
- /** initialize your data structure here. */
- stack <int> min_num , nums;
- MinStack() {
- stack <int> temp ;
- min_num = temp;
- nums = temp;
- }
- void push(int x) {
- nums.push(x);
- if (min_num.empty())
- {
- min_num.push(x);
- }
- else
- {
- min_num.push(min (x,min_num.top()));
- }
- }
- void pop() {
- min_num.pop();
- nums.pop();
- }
- int top() {
- return nums.top();
- }
- int getMin() {
- return min_num.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();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement