-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1838: Frequency of the Most Frequent Element
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/problem_1838_frequency_of_the_most_frequent_element/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/problem_1838_frequency_of_the_most_frequent_element/sliding_window.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/problem_1838_frequency_of_the_most_frequent_element/sliding_window_2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |