classFreqStack:def__init__(self):self.cnt=defaultdict(int)self.q=[]self.ts=0defpush(self,val:int)->None:self.ts+=1self.cnt[val]+=1heappush(self.q,(-self.cnt[val],-self.ts,val))defpop(self)->int:val=heappop(self.q)[2]self.cnt[val]-=1returnval# Your FreqStack object will be instantiated and called as such:# obj = FreqStack()# obj.push(val)# param_2 = obj.pop()
classFreqStack{privateMap<Integer,Integer>cnt=newHashMap<>();privatePriorityQueue<int[]>q=newPriorityQueue<>((a,b)->a[0]==b[0]?b[1]-a[1]:b[0]-a[0]);privateintts;publicFreqStack(){}publicvoidpush(intval){cnt.put(val,cnt.getOrDefault(val,0)+1);q.offer(newint[]{cnt.get(val),++ts,val});}publicintpop(){intval=q.poll()[2];cnt.put(val,cnt.get(val)-1);returnval;}}/** * Your FreqStack object will be instantiated and called as such: * FreqStack obj = new FreqStack(); * obj.push(val); * int param_2 = obj.pop(); */
classFreqStack{public:FreqStack(){}voidpush(intval){++cnt[val];q.emplace(cnt[val],++ts,val);}intpop(){auto[a,b,val]=q.top();q.pop();--cnt[val];returnval;}private:unordered_map<int,int>cnt;priority_queue<tuple<int,int,int>>q;intts=0;};/** * Your FreqStack object will be instantiated and called as such: * FreqStack* obj = new FreqStack(); * obj->push(val); * int param_2 = obj->pop(); */
typeFreqStackstruct{cntmap[int]intqhptsint}funcConstructor()FreqStack{returnFreqStack{map[int]int{},hp{},0}}func(this*FreqStack)Push(valint){this.cnt[val]++this.ts++heap.Push(&this.q,tuple{this.cnt[val],this.ts,val})}func(this*FreqStack)Pop()int{val:=heap.Pop(&this.q).(tuple).valthis.cnt[val]--returnval}typetuplestruct{cnt,ts,valint}typehp[]tuplefunc(hhp)Len()int{returnlen(h)}func(hhp)Less(i,jint)bool{returnh[i].cnt>h[j].cnt||h[i].cnt==h[j].cnt&&h[i].ts>h[j].ts}func(hhp)Swap(i,jint){h[i],h[j]=h[j],h[i]}func(h*hp)Push(vany){*h=append(*h,v.(tuple))}func(h*hp)Pop()any{a:=*h;v:=a[len(a)-1];*h=a[:len(a)-1];returnv}/** * Your FreqStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(val); * param_2 := obj.Pop(); */
classFreqStack:def__init__(self):self.cnt=defaultdict(int)self.d=defaultdict(list)self.mx=0defpush(self,val:int)->None:self.cnt[val]+=1self.d[self.cnt[val]].append(val)self.mx=max(self.mx,self.cnt[val])defpop(self)->int:val=self.d[self.mx].pop()self.cnt[val]-=1ifnotself.d[self.mx]:self.mx-=1returnval# Your FreqStack object will be instantiated and called as such:# obj = FreqStack()# obj.push(val)# param_2 = obj.pop()
classFreqStack{privateMap<Integer,Integer>cnt=newHashMap<>();privateMap<Integer,Deque<Integer>>d=newHashMap<>();privateintmx;publicFreqStack(){}publicvoidpush(intval){cnt.put(val,cnt.getOrDefault(val,0)+1);intt=cnt.get(val);d.computeIfAbsent(t,k->newArrayDeque<>()).push(val);mx=Math.max(mx,t);}publicintpop(){intval=d.get(mx).pop();cnt.put(val,cnt.get(val)-1);if(d.get(mx).isEmpty()){--mx;}returnval;}}/** * Your FreqStack object will be instantiated and called as such: * FreqStack obj = new FreqStack(); * obj.push(val); * int param_2 = obj.pop(); */
classFreqStack{public:FreqStack(){}voidpush(intval){++cnt[val];d[cnt[val]].push(val);mx=max(mx,cnt[val]);}intpop(){intval=d[mx].top();--cnt[val];d[mx].pop();if(d[mx].empty())--mx;returnval;}private:unordered_map<int,int>cnt;unordered_map<int,stack<int>>d;intmx=0;};/** * Your FreqStack object will be instantiated and called as such: * FreqStack* obj = new FreqStack(); * obj->push(val); * int param_2 = obj->pop(); */
typeFreqStackstruct{cntmap[int]intdmap[int][]intmxint}funcConstructor()FreqStack{returnFreqStack{map[int]int{},map[int][]int{},0}}func(this*FreqStack)Push(valint){this.cnt[val]++this.d[this.cnt[val]]=append(this.d[this.cnt[val]],val)this.mx=max(this.mx,this.cnt[val])}func(this*FreqStack)Pop()int{val:=this.d[this.mx][len(this.d[this.mx])-1]this.d[this.mx]=this.d[this.mx][:len(this.d[this.mx])-1]this.cnt[val]--iflen(this.d[this.mx])==0{this.mx--}returnval}/** * Your FreqStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(val); * param_2 := obj.Pop(); */