classCustomStack:def__init__(self,maxSize:int):self.stk=[0]*maxSizeself.add=[0]*maxSizeself.i=0defpush(self,x:int)->None:ifself.i<len(self.stk):self.stk[self.i]=xself.i+=1defpop(self)->int:ifself.i<=0:return-1self.i-=1ans=self.stk[self.i]+self.add[self.i]ifself.i>0:self.add[self.i-1]+=self.add[self.i]self.add[self.i]=0returnansdefincrement(self,k:int,val:int)->None:i=min(k,self.i)-1ifi>=0:self.add[i]+=val# Your CustomStack object will be instantiated and called as such:# obj = CustomStack(maxSize)# obj.push(x)# param_2 = obj.pop()# obj.increment(k,val)
classCustomStack{privateint[]stk;privateint[]add;privateinti;publicCustomStack(intmaxSize){stk=newint[maxSize];add=newint[maxSize];}publicvoidpush(intx){if(i<stk.length){stk[i++]=x;}}publicintpop(){if(i<=0){return-1;}intans=stk[--i]+add[i];if(i>0){add[i-1]+=add[i];}add[i]=0;returnans;}publicvoidincrement(intk,intval){if(i>0){add[Math.min(i,k)-1]+=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); */
classCustomStack{public:CustomStack(intmaxSize){stk.resize(maxSize);add.resize(maxSize);i=0;}voidpush(intx){if(i<stk.size()){stk[i++]=x;}}intpop(){if(i<=0){return-1;}intans=stk[--i]+add[i];if(i>0){add[i-1]+=add[i];}add[i]=0;returnans;}voidincrement(intk,intval){if(i>0){add[min(k,i)-1]+=val;}}private:vector<int>stk;vector<int>add;inti;};/** * 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); */
typeCustomStackstruct{stk[]intadd[]intiint}funcConstructor(maxSizeint)CustomStack{returnCustomStack{make([]int,maxSize),make([]int,maxSize),0}}func(this*CustomStack)Push(xint){ifthis.i<len(this.stk){this.stk[this.i]=xthis.i++}}func(this*CustomStack)Pop()int{ifthis.i<=0{return-1}this.i--ans:=this.stk[this.i]+this.add[this.i]ifthis.i>0{this.add[this.i-1]+=this.add[this.i]}this.add[this.i]=0returnans}func(this*CustomStack)Increment(kint,valint){ifthis.i>0{this.add[min(k,this.i)-1]+=val}}/** * Your CustomStack object will be instantiated and called as such: * obj := Constructor(maxSize); * obj.Push(x); * param_2 := obj.Pop(); * obj.Increment(k,val); */
classCustomStack{privatestk:number[];privateadd:number[];privatei:number;constructor(maxSize:number){this.stk=Array(maxSize).fill(0);this.add=Array(maxSize).fill(0);this.i=0;}push(x:number):void{if(this.i<this.stk.length){this.stk[this.i++]=x;}}pop():number{if(this.i<=0){return-1;}constans=this.stk[--this.i]+this.add[this.i];if(this.i>0){this.add[this.i-1]+=this.add[this.i];}this.add[this.i]=0;returnans;}increment(k:number,val:number):void{if(this.i>0){this.add[Math.min(this.i,k)-1]+=val;}}}/** * Your CustomStack object will be instantiated and called as such: * var obj = new CustomStack(maxSize) * obj.push(x) * var param_2 = obj.pop() * obj.increment(k,val) */