nikunjsoni

1381

Mar 22nd, 2021
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CustomStack {
  2. public:
  3.     vector<int> stk, inc;
  4.     int n;
  5.     int min(int a, int b){
  6.         return (a<b)?a:b;
  7.     }
  8.     CustomStack(int maxSize) {
  9.         n = maxSize;
  10.     }
  11.    
  12.     void push(int x) {
  13.         if(stk.size() < n) {
  14.             stk.push_back(x);
  15.             inc.push_back(0);
  16.         }
  17.     }
  18.    
  19.     int pop() {
  20.         int top, idx;
  21.         idx = stk.size()-1;
  22.         if(idx < 0) return -1;
  23.         top = stk[idx];
  24.         stk.pop_back();
  25.         inc.pop_back();
  26.         if(idx >= 1)
  27.             inc[idx-1] += inc[idx];
  28.         return (top+inc[idx]);
  29.     }
  30.    
  31.     void increment(int k, int val) {
  32.         k = min(k, stk.size())-1;
  33.         if(k >= 0) inc[k] += val;
  34.     }
  35. };
  36.  
  37. /**
  38.  * Your CustomStack object will be instantiated and called as such:
  39.  * CustomStack* obj = new CustomStack(maxSize);
  40.  * obj->push(x);
  41.  * int param_2 = obj->pop();
  42.  * obj->increment(k,val);
  43.  */
RAW Paste Data