From 547630c8ce0f685fd6aa3a34f67424656cd9a5c4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 21 Mar 2024 11:04:22 +0800 Subject: [PATCH] 20240321 Finished --- src/problem/mod.rs | 3 +- src/problem/p2671_frequency_tracker.rs | 104 +++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2671_frequency_tracker.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 884c7e6..4c2f5c4 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -79,4 +79,5 @@ mod p2684_maximum_number_of_moves_in_a_grid; mod p310_minimum_height_trees; mod p303_range_sum_query_immutable; mod p1793_maximum_score_of_a_good_subarray; -mod p1969_minimum_non_zero_product_of_the_array_elements; \ No newline at end of file +mod p1969_minimum_non_zero_product_of_the_array_elements; +mod p2671_frequency_tracker; \ No newline at end of file diff --git a/src/problem/p2671_frequency_tracker.rs b/src/problem/p2671_frequency_tracker.rs new file mode 100644 index 0000000..fa76ece --- /dev/null +++ b/src/problem/p2671_frequency_tracker.rs @@ -0,0 +1,104 @@ +/** + * [2671] Frequency Tracker + */ +pub struct Solution {} + + +// submission codes start here +use std::collections::{HashMap, HashSet}; + +struct FrequencyTracker { + number_map : HashMap, + frequency_map : HashMap>, +} + + +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl FrequencyTracker { + + fn new() -> Self { + FrequencyTracker { + number_map: HashMap::new(), + frequency_map: HashMap::new(), + } + } + + fn add(&mut self, number: i32) { + let mut entry = self.number_map.entry(number).or_insert(0); + + if *entry != 0 { + if let Some(set) = self.frequency_map.get_mut(&entry) { + set.remove(&number); + } + } + + *entry += 1; + let set = self.frequency_map.entry(*entry) + .or_insert(HashSet::new()); + set.insert(number); + } + + fn delete_one(&mut self, number: i32) { + if let Some(entry) = self.number_map.get_mut(&number) { + if *entry == 0 { + return; + } + + if let Some(set) = self.frequency_map.get_mut(&entry) { + set.remove(&number); + } + + *entry -= 1; + let set = self.frequency_map.entry(*entry) + .or_insert(HashSet::new()); + set.insert(number); + } + } + + fn has_frequency(&self, frequency: i32) -> bool { + let set = self.frequency_map.get(&frequency); + + match set { + Some(set) => set.len() != 0, + None => false + } + } +} + +/** + * Your FrequencyTracker object will be instantiated and called as such: + * let obj = FrequencyTracker::new(); + * obj.add(number); + * obj.delete_one(number); + * let ret_3: bool = obj.has_frequency(frequency); + */ + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2671() { + let mut tracker = FrequencyTracker::new(); + + tracker.add(10); + tracker.delete_one(5); + assert!(tracker.has_frequency(1)); + tracker.delete_one(10); + tracker.delete_one(9); + tracker.delete_one(10); + assert!(!tracker.has_frequency(1)); + tracker.add(4); + assert!(tracker.has_frequency(1)); + tracker.delete_one(4); + assert!(!tracker.has_frequency(1)); + assert!(!tracker.has_frequency(1)); + tracker.add(10); + assert!(tracker.has_frequency(1)); + } +}