-
Notifications
You must be signed in to change notification settings - Fork 3
/
2671.cpp
37 lines (32 loc) · 943 Bytes
/
2671.cpp
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
class FrequencyTracker {
public:
unordered_map<int, int> counts;
unordered_map<int, int> frequencies;
FrequencyTracker() {
}
void add(int number) {
int freq = counts[number];
counts[number]++;
int postFreq = counts[number];
if (freq > 0) frequencies[freq]--;
frequencies[postFreq]++;
}
void deleteOne(int number) {
int freq = counts[number];
if (freq == 0) return;
counts[number]--;
int postFreq = counts[number];
frequencies[freq]--;
if (postFreq > 0) frequencies[postFreq]++;
}
bool hasFrequency(int frequency) {
return frequencies[frequency] > 0;
}
};
/**
* Your FrequencyTracker object will be instantiated and called as such:
* FrequencyTracker* obj = new FrequencyTracker();
* obj->add(number);
* obj->deleteOne(number);
* bool param_3 = obj->hasFrequency(frequency);
*/