Byte by Byte

1381. Design a Stack With Increment Operation.cpp 본문

개발 로그/알고리즘

1381. Design a Stack With Increment Operation.cpp

CyberSoak 2021. 10. 7. 11:18

스택 구현 문제. 숙달이 중요한 것 같다.

class CustomStack {
public:
    int maxn;
    int top;
    int *st;
    
    CustomStack(int maxSize) {
        maxn = maxSize;
        st = new int[maxn];
        top = -1;
    }
    
    void push(int x) {
        if(top != maxn-1){
            st[++top] = x;
        }
    }
    
    int pop() {
        if(top==-1) return -1;
        return st[top--];
    }
    
    void increment(int k, int val) {
        int inc = min(k-1,top);
        for(int i =0; i<=inc;i++){
            st[i]+=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);
 */