From 647fbc935798d0810c52dada5c3c5f8286a49e1d Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sat, 11 Nov 2023 23:11:09 +0800 Subject: [PATCH] Add problem 1838: Frequency of the Most Frequent Element --- src/lib.rs | 1 + .../mod.rs | 23 +++++++++ .../sliding_window.rs | 44 +++++++++++++++++ .../sliding_window_2.rs | 47 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/problem_1838_frequency_of_the_most_frequent_element/mod.rs create mode 100644 src/problem_1838_frequency_of_the_most_frequent_element/sliding_window.rs create mode 100644 src/problem_1838_frequency_of_the_most_frequent_element/sliding_window_2.rs diff --git a/src/lib.rs b/src/lib.rs index d1ea9fec..046f9e7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1440,6 +1440,7 @@ pub mod problem_1827_minimum_operations_to_make_the_array_increasing; pub mod problem_1832_check_if_the_sentence_is_pangram; pub mod problem_1833_maximum_ice_cream_bars; pub mod problem_1837_sum_of_digits_in_base_k; +pub mod problem_1838_frequency_of_the_most_frequent_element; pub mod problem_1839_longest_substring_of_all_vowels_in_order; pub mod problem_1844_replace_all_digits_with_characters; pub mod problem_1845_seat_reservation_manager; diff --git a/src/problem_1838_frequency_of_the_most_frequent_element/mod.rs b/src/problem_1838_frequency_of_the_most_frequent_element/mod.rs new file mode 100644 index 00000000..b484d8e4 --- /dev/null +++ b/src/problem_1838_frequency_of_the_most_frequent_element/mod.rs @@ -0,0 +1,23 @@ +pub mod sliding_window; +pub mod sliding_window_2; + +pub trait Solution { + fn max_frequency(nums: Vec, k: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ((&[1, 2, 4] as &[_], 5), 3), + ((&[1, 4, 8, 13], 5), 2), + ((&[3, 9, 6], 2), 1), + ]; + + for ((nums, k), expected) in test_cases { + assert_eq!(S::max_frequency(nums.to_vec(), k), expected); + } + } +} diff --git a/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window.rs b/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window.rs new file mode 100644 index 00000000..cad251d4 --- /dev/null +++ b/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window.rs @@ -0,0 +1,44 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn max_frequency(nums: Vec, k: i32) -> i32 { + let mut nums = nums; + + nums.sort_unstable(); + + let mut start = 0_usize; + let mut sum = 0; + let mut result = 0; + + for (i, num) in (1..).zip(&nums) { + sum += num; + + while num * (i - start) as i32 - sum > k { + sum -= nums[start]; + start += 1; + } + + result = result.max(i - start); + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn max_frequency(nums: Vec, k: i32) -> i32 { + Self::max_frequency(nums, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window_2.rs b/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window_2.rs new file mode 100644 index 00000000..eb3196f9 --- /dev/null +++ b/src/problem_1838_frequency_of_the_most_frequent_element/sliding_window_2.rs @@ -0,0 +1,47 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn max_frequency(nums: Vec, k: i32) -> i32 { + let mut nums = nums; + + nums.sort_unstable(); + + let mut start = 0_usize; + let mut required = 0; + let mut result = 0; + let mut prev = 0; + + for (i, &num) in nums.iter().enumerate() { + required += (num - prev) * (i - start) as i32; + + while required > k { + required -= num - nums[start]; + start += 1; + } + + result = result.max(i + 1 - start); + + prev = num; + } + + result as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn max_frequency(nums: Vec, k: i32) -> i32 { + Self::max_frequency(nums, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}