Skip to content

Commit

Permalink
Add problem 1838: Frequency of the Most Frequent Element
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 11, 2023
1 parent 7835092 commit 647fbc9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/problem_1838_frequency_of_the_most_frequent_element/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub mod sliding_window;
pub mod sliding_window_2;

pub trait Solution {
fn max_frequency(nums: Vec<i32>, k: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn max_frequency(nums: Vec<i32>, 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<i32>, k: i32) -> i32 {
Self::max_frequency(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn max_frequency(nums: Vec<i32>, 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<i32>, k: i32) -> i32 {
Self::max_frequency(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}

0 comments on commit 647fbc9

Please sign in to comment.