2671. Frequency Tracker Medium

@problem@discussion
#Hash Table#Design



1/**
2 * [2671] Frequency Tracker
3 *
4 * Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.
5 * Implement the FrequencyTracker class.
6 * 
7 * 	FrequencyTracker(): Initializes the FrequencyTracker object with an empty array initially.
8 * 	void add(int number): Adds number to the data structure.
9 * 	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.
10 * 	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.
11 * 
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input
16 * ["FrequencyTracker", "add", "add", "hasFrequency"]
17 * [[], [3], [3], [2]]
18 * Output
19 * [null, null, null, true]
20 * Explanation
21 * FrequencyTracker frequencyTracker = new FrequencyTracker();
22 * frequencyTracker.add(3); // The data structure now contains [3]
23 * frequencyTracker.add(3); // The data structure now contains [3, 3]
24 * frequencyTracker.hasFrequency(2); // Returns true, because 3 occurs twice
25 * 
26 * <strong class="example">Example 2:
27 * 
28 * Input
29 * ["FrequencyTracker", "add", "deleteOne", "hasFrequency"]
30 * [[], [1], [1], [1]]
31 * Output
32 * [null, null, null, false]
33 * Explanation
34 * FrequencyTracker frequencyTracker = new FrequencyTracker();
35 * frequencyTracker.add(1); // The data structure now contains [1]
36 * frequencyTracker.deleteOne(1); // The data structure becomes empty []
37 * frequencyTracker.hasFrequency(1); // Returns false, because the data structure is empty
38 * 
39 * <strong class="example">Example 3:
40 * 
41 * Input
42 * ["FrequencyTracker", "hasFrequency", "add", "hasFrequency"]
43 * [[], [2], [3], [1]]
44 * Output
45 * [null, false, null, true]
46 * Explanation
47 * FrequencyTracker frequencyTracker = new FrequencyTracker();
48 * frequencyTracker.hasFrequency(2); // Returns false, because the data structure is empty
49 * frequencyTracker.add(3); // The data structure now contains [3]
50 * frequencyTracker.hasFrequency(1); // Returns true, because 3 occurs once
51 * 
52 *  
53 * Constraints:
54 * 
55 * 	1 <= number <= 10^5
56 * 	1 <= frequency <= 10^5
57 * 	At most, 2 * 10^5 calls will be made to add, deleteOne, and hasFrequency in total.
58 * 
59 */
60pub struct Solution {}
61
62// problem: https://leetcode.com/problems/frequency-tracker/
63// discuss: https://leetcode.com/problems/frequency-tracker/discuss/?currentPage=1&orderBy=most_votes&query=
64
65// submission codes start here
66
67struct FrequencyTracker {
68        false
69    }
70
71
72/** 
73 * `&self` means the method takes an immutable reference.
74 * If you need a mutable reference, change it to `&mut self` instead.
75 */
76impl FrequencyTracker {
77
78    fn new() -> Self {
79        
80    }
81    
82    fn add(&self, number: i32) {
83        
84    }
85    
86    fn delete_one(&self, number: i32) {
87        
88    }
89    
90    fn has_frequency(&self, frequency: i32) -> bool {
91        
92    }
93}
94
95/**
96 * Your FrequencyTracker object will be instantiated and called as such:
97 * let obj = FrequencyTracker::new();
98 * obj.add(number);
99 * obj.delete_one(number);
100 * let ret_3: bool = obj.has_frequency(frequency);
101 */
102
103// submission codes end
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn test_2671() {
111    }
112}
113


Back
© 2025 bowen.ge All Rights Reserved.