2671. Frequency Tracker
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 28, 2024
Last updated : June 28, 2024
Related Topics : Hash Table, Design
Acceptance Rate : 29.843 %
class FrequencyTracker:
def __init__(self):
self.valToFrequency = defaultdict(int)
self.frequencyFrequencies = defaultdict(int)
def add(self, number: int) -> None:
if self.valToFrequency[number] != 0 :
self.frequencyFrequencies[self.valToFrequency[number]] -= 1
self.valToFrequency[number] += 1
self.frequencyFrequencies[self.valToFrequency[number]] += 1
def deleteOne(self, number: int) -> None:
if self.valToFrequency[number] <= 0 :
return
self.frequencyFrequencies[self.valToFrequency[number]] -= 1
self.valToFrequency[number] -= 1
if self.valToFrequency[number] > 0 :
self.frequencyFrequencies[self.valToFrequency[number]] += 1
def hasFrequency(self, frequency: int) -> bool:
return self.frequencyFrequencies[frequency] > 0
# Your FrequencyTracker object will be instantiated and called as such:
# obj = FrequencyTracker()
# obj.add(number)
# obj.deleteOne(number)
# param_3 = obj.hasFrequency(frequency)