classMinStack:def__init__(self):self.stk1=[]self.stk2=[inf]defpush(self,val:int)->None:self.stk1.append(val)self.stk2.append(min(val,self.stk2[-1]))defpop(self)->None:self.stk1.pop()self.stk2.pop()deftop(self)->int:returnself.stk1[-1]defgetMin(self)->int:returnself.stk2[-1]# Your MinStack object will be instantiated and called as such:# obj = MinStack()# obj.push(val)# obj.pop()# param_3 = obj.top()# param_4 = obj.getMin()
classMinStack{privateDeque<Integer>stk1=newArrayDeque<>();privateDeque<Integer>stk2=newArrayDeque<>();publicMinStack(){stk2.push(Integer.MAX_VALUE);}publicvoidpush(intval){stk1.push(val);stk2.push(Math.min(val,stk2.peek()));}publicvoidpop(){stk1.pop();stk2.pop();}publicinttop(){returnstk1.peek();}publicintgetMin(){returnstk2.peek();}}/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(val); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
classMinStack{public:MinStack(){stk2.push(INT_MAX);}voidpush(intval){stk1.push(val);stk2.push(min(val,stk2.top()));}voidpop(){stk1.pop();stk2.pop();}inttop(){returnstk1.top();}intgetMin(){returnstk2.top();}private:stack<int>stk1;stack<int>stk2;};/** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(val); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->getMin(); */
typeMinStackstruct{stk1[]intstk2[]int}funcConstructor()MinStack{returnMinStack{[]int{},[]int{math.MaxInt32}}}func(this*MinStack)Push(valint){this.stk1=append(this.stk1,val)this.stk2=append(this.stk2,min(val,this.stk2[len(this.stk2)-1]))}func(this*MinStack)Pop(){this.stk1=this.stk1[:len(this.stk1)-1]this.stk2=this.stk2[:len(this.stk2)-1]}func(this*MinStack)Top()int{returnthis.stk1[len(this.stk1)-1]}func(this*MinStack)GetMin()int{returnthis.stk2[len(this.stk2)-1]}/** * Your MinStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(val); * obj.Pop(); * param_3 := obj.Top(); * param_4 := obj.GetMin(); */
classMinStack{stk1:number[];stk2:number[];constructor(){this.stk1=[];this.stk2=[Infinity];}push(val:number):void{this.stk1.push(val);this.stk2.push(Math.min(val,this.stk2[this.stk2.length-1]));}pop():void{this.stk1.pop();this.stk2.pop();}top():number{returnthis.stk1[this.stk1.length-1];}getMin():number{returnthis.stk2[this.stk2.length-1];}}/** * Your MinStack object will be instantiated and called as such: * var obj = new MinStack() * obj.push(x) * obj.pop() * var param_3 = obj.top() * var param_4 = obj.getMin() */
usestd::collections::VecDeque;structMinStack{stk1:VecDeque<i32>,stk2:VecDeque<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implMinStack{fnnew()->Self{Self{stk1:VecDeque::new(),stk2:VecDeque::new(),}}fnpush(&mutself,x:i32){self.stk1.push_back(x);ifself.stk2.is_empty()||*self.stk2.back().unwrap()>=x{self.stk2.push_back(x);}}fnpop(&mutself){letval=self.stk1.pop_back().unwrap();if*self.stk2.back().unwrap()==val{self.stk2.pop_back();}}fntop(&self)->i32{*self.stk1.back().unwrap()}fnget_min(&self)->i32{*self.stk2.back().unwrap()}}
varMinStack=function(){this.stk1=[];this.stk2=[Infinity];};/** * @param {number} val * @return {void} */MinStack.prototype.push=function(val){this.stk1.push(val);this.stk2.push(Math.min(this.stk2[this.stk2.length-1],val));};/** * @return {void} */MinStack.prototype.pop=function(){this.stk1.pop();this.stk2.pop();};/** * @return {number} */MinStack.prototype.top=function(){returnthis.stk1[this.stk1.length-1];};/** * @return {number} */MinStack.prototype.getMin=function(){returnthis.stk2[this.stk2.length-1];};/** * Your MinStack object will be instantiated and called as such: * var obj = new MinStack() * obj.push(val) * obj.pop() * var param_3 = obj.top() * var param_4 = obj.getMin() */
publicclassMinStack{privateStack<int>stk1=newStack<int>();privateStack<int>stk2=newStack<int>();publicMinStack(){stk2.Push(int.MaxValue);}publicvoidPush(intx){stk1.Push(x);stk2.Push(Math.Min(x,GetMin()));}publicvoidPop(){stk1.Pop();stk2.Pop();}publicintTop(){returnstk1.Peek();}publicintGetMin(){returnstk2.Peek();}}/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.Push(x); * obj.Pop(); * int param_3 = obj.Top(); * int param_4 = obj.GetMin(); */