Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomStack {
- public:
- vector<int> stk, inc;
- int n;
- int min(int a, int b){
- return (a<b)?a:b;
- }
- CustomStack(int maxSize) {
- n = maxSize;
- }
- void push(int x) {
- if(stk.size() < n) {
- stk.push_back(x);
- inc.push_back(0);
- }
- }
- int pop() {
- int top, idx;
- idx = stk.size()-1;
- if(idx < 0) return -1;
- top = stk[idx];
- stk.pop_back();
- inc.pop_back();
- if(idx >= 1)
- inc[idx-1] += inc[idx];
- return (top+inc[idx]);
- }
- void increment(int k, int val) {
- k = min(k, stk.size())-1;
- if(k >= 0) inc[k] += val;
- }
- };
- /**
- * Your CustomStack object will be instantiated and called as such:
- * CustomStack* obj = new CustomStack(maxSize);
- * obj->push(x);
- * int param_2 = obj->pop();
- * obj->increment(k,val);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement