knakul853

Untitled

Jul 20th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. class MinStack {
  2. public:
  3.     /** initialize your data structure here. */
  4.     vector<int>s1,s2;
  5.     MinStack() {
  6.  
  7.     }
  8.    
  9.     void push(int x) {
  10.         s1.push_back(x);
  11.        
  12.         if(s2.empty() || x <= getMin())s2.push_back(x);
  13.        
  14.     }
  15.    
  16.     void pop() {
  17.         if(s1.back() == getMin())s2.pop_back();
  18.         s1.pop_back();
  19.        
  20.     }
  21.    
  22.     int top() {
  23.         return s1.back();
  24.    
  25.        
  26.     }
  27.    
  28.     int getMin() {
  29.         return s2.back();
  30.        
  31.     }
  32. };
  33.  
  34. /**
  35.  * Your MinStack object will be instantiated and called as such:
  36.  * MinStack* obj = new MinStack();
  37.  * obj->push(x);
  38.  * obj->pop();
  39.  * int param_3 = obj->top();
  40.  * int param_4 = obj->getMin();
  41.  */
Add Comment
Please, Sign In to add comment