Talentd
FleetCode

432. All O`one Data Structure

Hard44.2% acceptance13 companies

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.

Example 1:

Input
["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"]
[[], ["hello"], ["hello"], [], [], ["leet"], [], []]
Output
[null, null, null, "hello", "hello", null, "hello", "leet"]

Explanation
AllOne allOne = new AllOne();
allOne.inc("hello");
allOne.inc("hello");
allOne.getMaxKey(); // return "hello"
allOne.getMinKey(); // return "hello"
allOne.inc("leet");
allOne.getMaxKey(); // return "hello"
allOne.getMinKey(); // return "leet"

Constraints:

  • 1 <= key.length <= 10
  • key consists of lowercase English letters.
  • 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.
47:29
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class AllOne:
2
3 def __init__(self):
4
5
6 def inc(self, key: str) -> None:
7
8
9 def dec(self, key: str) -> None:
10
11
12 def getMaxKey(self) -> str:
13
14
15 def getMinKey(self) -> str:
16
17
18
19# Your AllOne object will be instantiated and called as such:
20# obj = AllOne()
21# obj.inc(key)
22# obj.dec(key)
23# param_3 = obj.getMaxKey()
24# param_4 = obj.getMinKey()
Starter codePython 3Spaces: 4
Examples
Input
["AllOne","inc","inc","getMaxKey","getMinKey","inc","getMaxKey","getMinKey"]
[[],["hello"],["hello"],[],[],["leet"],[],[]]
Your code is saved on this device.Solution