Talentd
FleetCode

2671. Frequency Tracker

Medium31.6% acceptance

Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.

Implement the FrequencyTracker class.

  • FrequencyTracker(): Initializes the FrequencyTracker object with an empty array initially.
  • void add(int number): Adds number to the data structure.
  • void deleteOne(int number): Deletes one occurrence of number from the data structure. The data structure may not contain number, and in this case nothing is deleted.
  • bool hasFrequency(int frequency): Returns true if there is a number in the data structure that occurs frequency number of times, otherwise, it returns false.

Example 1:

Input
["FrequencyTracker", "add", "add", "hasFrequency"]
[[], [3], [3], [2]]
Output
[null, null, null, true]

Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.add(3); // The data structure now contains [3]
frequencyTracker.add(3); // The data structure now contains [3, 3]
frequencyTracker.hasFrequency(2); // Returns true, because 3 occurs twice

Example 2:

Input
["FrequencyTracker", "add", "deleteOne", "hasFrequency"]
[[], [1], [1], [1]]
Output
[null, null, null, false]

Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.add(1); // The data structure now contains [1]
frequencyTracker.deleteOne(1); // The data structure becomes empty []
frequencyTracker.hasFrequency(1); // Returns false, because the data structure is empty

Example 3:

Input
["FrequencyTracker", "hasFrequency", "add", "hasFrequency"]
[[], [2], [3], [1]]
Output
[null, false, null, true]

Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.hasFrequency(2); // Returns false, because the data structure is empty
frequencyTracker.add(3); // The data structure now contains [3]
frequencyTracker.hasFrequency(1); // Returns true, because 3 occurs once

Constraints:

  • 1 <= number <= 105
  • 1 <= frequency <= 105
  • At most, 2 * 105 calls will be made to add, deleteOne, and hasFrequency in total.

Hints open one at a time

Hint 1
Put all the numbers in a hash map (or just an integer array given the number range is small) to maintain each number’s frequency dynamically.
Hint 2
Put each frequency in another hash map (or just an integer array given the range is small, note there are only 200000 calls in total) to maintain each kind of frequency dynamically.
Hint 3
Keep the 2 hash maps in sync.
8:58
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class FrequencyTracker:
2
3 def __init__(self):
4
5
6 def add(self, number: int) -> None:
7
8
9 def deleteOne(self, number: int) -> None:
10
11
12 def hasFrequency(self, frequency: int) -> bool:
13
14
15
16# Your FrequencyTracker object will be instantiated and called as such:
17# obj = FrequencyTracker()
18# obj.add(number)
19# obj.deleteOne(number)
20# param_3 = obj.hasFrequency(frequency)
Starter codePython 3Spaces: 4
Examples
Input
["FrequencyTracker","add","add","hasFrequency"]
[[],[3],[3],[2]]
Input
["FrequencyTracker","add","deleteOne","hasFrequency"]
[[],[1],[1],[1]]
Input
["FrequencyTracker","hasFrequency","add","hasFrequency"]
[[],[2],[3],[1]]
Your code is saved on this device.Solution