fromsortedcontainersimportSortedListclassNode:def__init__(self,val=0):self.val=valself.prev:Union[Node,None]=Noneself.next:Union[Node,None]=NoneclassDoubleLinkedList:def__init__(self):self.head=Node()self.tail=Node()self.head.next=self.tailself.tail.prev=self.headdefappend(self,val)->Node:node=Node(val)node.next=self.tailnode.prev=self.tail.prevself.tail.prev=nodenode.prev.next=nodereturnnode@staticmethoddefremove(node)->Node:node.prev.next=node.nextnode.next.prev=node.prevreturnnodedefpop(self)->Node:returnself.remove(self.tail.prev)defpeek(self):returnself.tail.prev.valclassMaxStack:def__init__(self):self.stk=DoubleLinkedList()self.sl=SortedList(key=lambdax:x.val)defpush(self,x:int)->None:node=self.stk.append(x)self.sl.add(node)defpop(self)->int:node=self.stk.pop()self.sl.remove(node)returnnode.valdeftop(self)->int:returnself.stk.peek()defpeekMax(self)->int:returnself.sl[-1].valdefpopMax(self)->int:node=self.sl.pop()DoubleLinkedList.remove(node)returnnode.val# Your MaxStack object will be instantiated and called as such:# obj = MaxStack()# obj.push(x)# param_2 = obj.pop()# param_3 = obj.top()# param_4 = obj.peekMax()# param_5 = obj.popMax()
classNode{publicintval;publicNodeprev,next;publicNode(){}publicNode(intval){this.val=val;}}classDoubleLinkedList{privatefinalNodehead=newNode();privatefinalNodetail=newNode();publicDoubleLinkedList(){head.next=tail;tail.prev=head;}publicNodeappend(intval){Nodenode=newNode(val);node.next=tail;node.prev=tail.prev;tail.prev=node;node.prev.next=node;returnnode;}publicstaticNoderemove(Nodenode){node.prev.next=node.next;node.next.prev=node.prev;returnnode;}publicNodepop(){returnremove(tail.prev);}publicintpeek(){returntail.prev.val;}}classMaxStack{privateDoubleLinkedListstk=newDoubleLinkedList();privateTreeMap<Integer,List<Node>>tm=newTreeMap<>();publicMaxStack(){}publicvoidpush(intx){Nodenode=stk.append(x);tm.computeIfAbsent(x,k->newArrayList<>()).add(node);}publicintpop(){Nodenode=stk.pop();List<Node>nodes=tm.get(node.val);intx=nodes.remove(nodes.size()-1).val;if(nodes.isEmpty()){tm.remove(node.val);}returnx;}publicinttop(){returnstk.peek();}publicintpeekMax(){returntm.lastKey();}publicintpopMax(){intx=peekMax();List<Node>nodes=tm.get(x);Nodenode=nodes.remove(nodes.size()-1);if(nodes.isEmpty()){tm.remove(x);}DoubleLinkedList.remove(node);returnx;}}/** * Your MaxStack object will be instantiated and called as such: * MaxStack obj = new MaxStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.peekMax(); * int param_5 = obj.popMax(); */
classMaxStack{public:MaxStack(){}voidpush(intx){stk.push_back(x);tm.insert({x,--stk.end()});}intpop(){autoit=--stk.end();intans=*it;automit=--tm.upper_bound(ans);tm.erase(mit);stk.erase(it);returnans;}inttop(){returnstk.back();}intpeekMax(){returntm.rbegin()->first;}intpopMax(){automit=--tm.end();autoit=mit->second;intans=*it;tm.erase(mit);stk.erase(it);returnans;}private:multimap<int,list<int>::iterator>tm;list<int>stk;};/** * Your MaxStack object will be instantiated and called as such: * MaxStack* obj = new MaxStack(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->peekMax(); * int param_5 = obj->popMax(); */