-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frequency Tracker.py
45 lines (30 loc) · 1.41 KB
/
Frequency Tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
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 occurence 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.
'''
from collections import defaultdict
class FrequencyTracker:
def __init__(self):
self.ds = defaultdict(int)
self.fs = defaultdict(int)
def add(self, number: int) -> None:
self.fs[self.ds[number]] -=1
self.ds[number]+=1
self.fs[self.ds[number]]+=1
def deleteOne(self, number: int) -> None:
self.fs[self.ds[number]]-=1
self.ds[number] = max(0, self.ds[number]-1)
self.fs[self.ds[number]]+=1
def hasFrequency(self, frequency: int) -> bool:
if self.fs[frequency]>0:
return True
return False
# Your FrequencyTracker object will be instantiated and called as such:
# obj = FrequencyTracker()
# obj.add(number)
# obj.deleteOne(number)
# param_3 = obj.hasFrequency(frequency)