Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
Implement the AllOne class:
AllOne() Initializes the object of the data structure.
inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1.
dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "".
getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".
Note that each function must run in O(1) average time complexity.
It is guaranteed that for each call to dec, key is existing in the data structure.
At most 5 * 104 calls will be made to inc, dec, getMaxKey, and getMinKey.
Solutions
Solution 1
Thinking
\(\textit{inc}\), \(\textit{dec}\), \(\textit{getMaxKey}\) and \(\textit{getMinKey}\) all need amortized \(O(1)\). A hash map updates a count in \(O(1)\) but cannot report the extremes; an ordered set would take logarithmic time.
Keys that share a count sit in one doubly linked bucket, and buckets form a ring ordered by count. A map sends each key to its bucket. Increment and decrement move a key only to an adjacent count, inserting or deleting an empty bucket when needed.
The ring's successor and predecessor are the min and max counts. Keys never jump over buckets, so the list is never scanned.
classNode:def__init__(self,key='',cnt=0):self.prev=Noneself.next=Noneself.cnt=cntself.keys={key}definsert(self,node):node.prev=selfnode.next=self.nextnode.prev.next=nodenode.next.prev=nodereturnnodedefremove(self):self.prev.next=self.nextself.next.prev=self.prevclassAllOne:def__init__(self):self.root=Node()self.root.next=self.rootself.root.prev=self.rootself.nodes={}definc(self,key:str)->None:root,nodes=self.root,self.nodesifkeynotinnodes:ifroot.next==rootorroot.next.cnt>1:nodes[key]=root.insert(Node(key,1))else:root.next.keys.add(key)nodes[key]=root.nextelse:curr=nodes[key]next=curr.nextifnext==rootornext.cnt>curr.cnt+1:nodes[key]=curr.insert(Node(key,curr.cnt+1))else:next.keys.add(key)nodes[key]=nextcurr.keys.discard(key)ifnotcurr.keys:curr.remove()defdec(self,key:str)->None:root,nodes=self.root,self.nodescurr=nodes[key]ifcurr.cnt==1:nodes.pop(key)else:prev=curr.previfprev==rootorprev.cnt<curr.cnt-1:nodes[key]=prev.insert(Node(key,curr.cnt-1))else:prev.keys.add(key)nodes[key]=prevcurr.keys.discard(key)ifnotcurr.keys:curr.remove()defgetMaxKey(self)->str:returnnext(iter(self.root.prev.keys))defgetMinKey(self)->str:returnnext(iter(self.root.next.keys))# Your AllOne object will be instantiated and called as such:# obj = AllOne()# obj.inc(key)# obj.dec(key)# param_3 = obj.getMaxKey()# param_4 = obj.getMinKey()
classAllOne{Noderoot=newNode();Map<String,Node>nodes=newHashMap<>();publicAllOne(){root.next=root;root.prev=root;}publicvoidinc(Stringkey){if(!nodes.containsKey(key)){if(root.next==root||root.next.cnt>1){nodes.put(key,root.insert(newNode(key,1)));}else{root.next.keys.add(key);nodes.put(key,root.next);}}else{Nodecurr=nodes.get(key);Nodenext=curr.next;if(next==root||next.cnt>curr.cnt+1){nodes.put(key,curr.insert(newNode(key,curr.cnt+1)));}else{next.keys.add(key);nodes.put(key,next);}curr.keys.remove(key);if(curr.keys.isEmpty()){curr.remove();}}}publicvoiddec(Stringkey){Nodecurr=nodes.get(key);if(curr.cnt==1){nodes.remove(key);}else{Nodeprev=curr.prev;if(prev==root||prev.cnt<curr.cnt-1){nodes.put(key,prev.insert(newNode(key,curr.cnt-1)));}else{prev.keys.add(key);nodes.put(key,prev);}}curr.keys.remove(key);if(curr.keys.isEmpty()){curr.remove();}}publicStringgetMaxKey(){returnroot.prev.keys.iterator().next();}publicStringgetMinKey(){returnroot.next.keys.iterator().next();}}classNode{Nodeprev;Nodenext;intcnt;Set<String>keys=newHashSet<>();publicNode(){this("",0);}publicNode(Stringkey,intcnt){this.cnt=cnt;keys.add(key);}publicNodeinsert(Nodenode){node.prev=this;node.next=this.next;node.prev.next=node;node.next.prev=node;returnnode;}publicvoidremove(){this.prev.next=this.next;this.next.prev=this.prev;}}/** * Your AllOne object will be instantiated and called as such: * AllOne obj = new AllOne(); * obj.inc(key); * obj.dec(key); * String param_3 = obj.getMaxKey(); * String param_4 = obj.getMinKey(); */