-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2671-FrequencyTracker.go
More file actions
116 lines (100 loc) · 3.87 KB
/
2671-FrequencyTracker.go
File metadata and controls
116 lines (100 loc) · 3.87 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
// 2671. Frequency Tracker
// 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 <= 10^5
// 1 <= frequency <= 10^5
// At most, 2 * 10^5 calls will be made to add, deleteOne, and hasFrequency in total.
import "fmt"
type FrequencyTracker struct {
data map[int]int
freq map[int]int
}
func Constructor() FrequencyTracker {
return FrequencyTracker{
data: map[int]int{},
freq: map[int]int{},
}
}
func (ft *FrequencyTracker) Add(number int) {
if ft.data[number] >= 1 {
ft.freq[ft.data[number]]--
}
ft.data[number]++
ft.freq[ft.data[number]]++
}
func (ft *FrequencyTracker) DeleteOne(number int) {
if ft.data[number] == 0 {
return
}
ft.freq[ft.data[number]]--
ft.data[number]--
if ft.data[number] != 0 {
ft.freq[ft.data[number]]++
} else {
delete(ft.data, number)
}
}
func (ft *FrequencyTracker) HasFrequency(frequency int) bool {
return ft.freq[frequency] > 0
}
/**
* Your FrequencyTracker object will be instantiated and called as such:
* obj := Constructor();
* obj.Add(number);
* obj.DeleteOne(number);
* param_3 := obj.HasFrequency(frequency);
*/
func main() {
obj := Constructor()
obj.Add(3) // The data structure now contains [3]
obj.Add(3) // The data structure now contains [3, 3]
fmt.Println(obj.HasFrequency(2)) // Returns true, because 3 occurs twice
// FrequencyTracker frequencyTracker = new FrequencyTracker();
obj = Constructor()
obj.Add(1) // The data structure now contains [1]
obj.DeleteOne(1); // The data structure becomes empty []
fmt.Println(obj.HasFrequency(1)) // Returns false, because the data structure is empty
obj = Constructor()
fmt.Println(obj.HasFrequency(2)) // Returns false, because the data structure is empty
obj.Add(3) // The data structure now contains [3]
fmt.Println(obj.HasFrequency(1)) // Returns true, because 3 occurs once
}