classMaxQueue:def__init__(self):self.q1=deque()self.q2=deque()defmax_value(self)->int:return-1ifnotself.q2elseself.q2[0]defpush_back(self,value:int)->None:whileself.q2andself.q2[-1]<value:self.q2.pop()self.q1.append(value)self.q2.append(value)defpop_front(self)->int:ifnotself.q1:return-1ans=self.q1.popleft()ifself.q2[0]==ans:self.q2.popleft()returnans# Your MaxQueue object will be instantiated and called as such:# obj = MaxQueue()# param_1 = obj.max_value()# obj.push_back(value)# param_3 = obj.pop_front()
classMaxQueue{privateDeque<Integer>q1=newArrayDeque<>();privateDeque<Integer>q2=newArrayDeque<>();publicMaxQueue(){}publicintmax_value(){returnq2.isEmpty()?-1:q2.peek();}publicvoidpush_back(intvalue){while(!q2.isEmpty()&&q2.peekLast()<value){q2.pollLast();}q1.offer(value);q2.offer(value);}publicintpop_front(){if(q1.isEmpty()){return-1;}intans=q1.poll();if(q2.peek()==ans){q2.poll();}returnans;}}/** * Your MaxQueue object will be instantiated and called as such: * MaxQueue obj = new MaxQueue(); * int param_1 = obj.max_value(); * obj.push_back(value); * int param_3 = obj.pop_front(); */
classMaxQueue{public:MaxQueue(){}intmax_value(){returnq2.empty()?-1:q2.front();}voidpush_back(intvalue){while(!q2.empty()&&q2.back()<value){q2.pop_back();}q1.push(value);q2.push_back(value);}intpop_front(){if(q1.empty()){return-1;}intans=q1.front();q1.pop();if(q2.front()==ans){q2.pop_front();}returnans;}private:queue<int>q1;deque<int>q2;};/** * Your MaxQueue object will be instantiated and called as such: * MaxQueue* obj = new MaxQueue(); * int param_1 = obj->max_value(); * obj->push_back(value); * int param_3 = obj->pop_front(); */
typeMaxQueuestruct{q1,q2[]int}funcConstructor()MaxQueue{returnMaxQueue{[]int{},[]int{}}}func(this*MaxQueue)Max_value()int{iflen(this.q2)==0{return-1}returnthis.q2[0]}func(this*MaxQueue)Push_back(valueint){forlen(this.q2)>0&&this.q2[len(this.q2)-1]<value{this.q2=this.q2[:len(this.q2)-1]}this.q1=append(this.q1,value)this.q2=append(this.q2,value)}func(this*MaxQueue)Pop_front()int{iflen(this.q1)==0{return-1}ans:=this.q1[0]this.q1=this.q1[1:]ifthis.q2[0]==ans{this.q2=this.q2[1:]}returnans}/** * Your MaxQueue object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Max_value(); * obj.Push_back(value); * param_3 := obj.Pop_front(); */
classMaxQueue{privatequeue:number[];privatedeque:number[];constructor(){this.queue=[];this.deque=[];}max_value():number{returnthis.deque[0]??-1;}push_back(value:number):void{this.queue.push(value);while(this.deque.length!==0&&this.deque[this.deque.length-1]<value){this.deque.pop();}this.deque.push(value);}pop_front():number{constres=this.queue.shift();if(res===this.deque[0]){this.deque.shift();}returnres??-1;}}/** * Your MaxQueue object will be instantiated and called as such: * var obj = new MaxQueue() * var param_1 = obj.max_value() * obj.push_back(value) * var param_3 = obj.pop_front() */
usestd::collections::VecDeque;structMaxQueue{queue:VecDeque<i32>,deque:VecDeque<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implMaxQueue{fnnew()->Self{Self{queue:VecDeque::new(),deque:VecDeque::new(),}}fnmax_value(&self)->i32{*self.deque.front().unwrap_or(&-1)}fnpush_back(&mutself,value:i32){self.queue.push_back(value);while!self.deque.is_empty()&&*self.deque.back().unwrap()<value{self.deque.pop_back();}self.deque.push_back(value);}fnpop_front(&mutself)->i32{ifself.queue.is_empty(){return-1;}letres=self.queue.pop_front().unwrap();ifres==self.deque[0]{self.deque.pop_front();}res}}
varMaxQueue=function(){this.q1=[];this.q2=[];};/** * @return {number} */MaxQueue.prototype.max_value=function(){returnthis.q2.length?this.q2[0]:-1;};/** * @param {number} value * @return {void} */MaxQueue.prototype.push_back=function(value){while(this.q2.length&&this.q2[this.q2.length-1]<value){this.q2.pop();}this.q1.push(value);this.q2.push(value);};/** * @return {number} */MaxQueue.prototype.pop_front=function(){if(!this.q1.length){return-1;}constans=this.q1.shift();if(this.q2[0]==ans){this.q2.shift();}returnans;};/** * Your MaxQueue object will be instantiated and called as such: * var obj = new MaxQueue() * var param_1 = obj.max_value() * obj.push_back(value) * var param_3 = obj.pop_front() */
publicclassMaxQueue{LinkedList<int>mvq;Queue<int>q;publicMaxQueue(){mvq=newLinkedList<int>();q=newQueue<int>();}publicintMax_value(){if(mvq.Count==0){return-1;}returnmvq.First.Value;}publicvoidPush_back(intvalue){q.Enqueue(value);while(mvq.Count>0&&mvq.Last.Value<value){mvq.RemoveLast();}mvq.AddLast(value);}publicintPop_front(){if(q.Count==0){return-1;}intv=q.Dequeue();if(mvq.First.Value==v){mvq.RemoveFirst();}returnv;}}/** * Your MaxQueue object will be instantiated and called as such: * MaxQueue obj = new MaxQueue(); * int param_1 = obj.Max_value(); * obj.Push_back(value); * int param_3 = obj.Pop_front(); */
classMaxQueue{privatevarq1:[Int]=[]privatevarq2:[Int]=[]init(){}funcmax_value()->Int{returnq2.isEmpty?-1:q2.first!}funcpush_back(_value:Int){q1.append(value)while!q2.isEmpty&&q2.last!<value{q2.removeLast()}q2.append(value)}funcpop_front()->Int{ifq1.isEmpty{return-1}letans=q1.removeFirst()ifq2.first==ans{q2.removeFirst()}returnans}}/** * Your MaxQueue object will be instantiated and called as such: * let obj = MaxQueue(); * let param_1 = obj.max_value(); * obj.push_back(value); * let param_3 = obj.pop_front(); */