knakul853

Untitled

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