Implement AllOne to maintain counts for string keys and retrieve a key with the smallest or largest count. Each method must run in O(1) average time.
public AllOne()
public void inc(String key)
key: Increase its count by 1. If absent, insert it with count 1.public void dec(String key)
key: Decrease its count by 1. Remove it when its count becomes 0.public String getMaxKey()
"" if the structure is empty.public String getMinKey()
"" if the structure is empty.If multiple keys share the requested count, return the key that reached that count earliest. Leaving and later returning to a count resets this order. Getter calls do not change the order.
1 ≤ key.length() ≤ 10.key contains only lowercase English letters.dec always exists.50,000 calls to inc, dec, getMaxKey, and getMinKey combined.Parameter names in example calls are shown for readability and are not Java syntax. Calls to inc and dec return no value.
AllOne counts = new AllOne()counts.inc(key = "plum")counts.inc(key = "kiwi")counts.getMaxKey() returns "plum".counts.inc(key = "kiwi")counts.getMaxKey() returns "kiwi".counts.getMinKey() returns "plum".counts.inc(key = "plum")counts.getMinKey() returns "kiwi".counts.dec(key = "plum")counts.getMinKey() returns "plum".counts.dec(key = "kiwi")counts.getMaxKey() returns "plum".When both counts become 2, "kiwi" wins the tie. After both decrease to 1, "plum" wins because it returned to that count first.
AllOne counts = new AllOne()counts.getMinKey() returns "".counts.inc(key = "oak")counts.dec(key = "oak")counts.getMaxKey() returns "".counts.inc(key = "elm")counts.inc(key = "oak")counts.getMinKey() returns "elm".counts.getMaxKey() returns "elm".counts.dec(key = "elm")counts.dec(key = "oak")counts.getMinKey() returns "".A removed key starts again at count 1 when reinserted. The earlier "oak" insertion does not affect the tie with "elm".