Skip to content

Commit eaeba82

Browse files
committed
Add problem 3002: Maximum Size of a Set After Removals
1 parent 5d48d81 commit eaeba82

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ pub mod problem_2996_smallest_missing_integer_greater_than_sequential_prefix_sum
20432043
pub mod problem_2997_minimum_number_of_operations_to_make_array_xor_equal_to_k;
20442044
pub mod problem_3000_maximum_area_of_longest_diagonal_rectangle;
20452045
pub mod problem_3001_minimum_moves_to_capture_the_queen;
2046+
pub mod problem_3005_count_elements_with_maximum_frequency;
20462047

20472048
#[cfg(test)]
20482049
mod test_utilities;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::cmp::Ordering;
6+
7+
impl Solution {
8+
pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
9+
let mut max_frequency = 0;
10+
let mut max_frequency_sum = 0;
11+
let mut frequencies = [0_u8; 100];
12+
13+
for num in nums {
14+
let frequency = &mut frequencies[num.cast_unsigned() as usize - 1];
15+
16+
*frequency += 1;
17+
18+
let frequency = u32::from(*frequency);
19+
20+
match frequency.cmp(&max_frequency) {
21+
Ordering::Less => {}
22+
Ordering::Equal => max_frequency_sum += max_frequency,
23+
Ordering::Greater => {
24+
max_frequency = frequency;
25+
max_frequency_sum = max_frequency;
26+
}
27+
}
28+
}
29+
30+
max_frequency_sum.cast_signed()
31+
}
32+
}
33+
34+
// ------------------------------------------------------ snip ------------------------------------------------------ //
35+
36+
impl super::Solution for Solution {
37+
fn max_frequency_elements(nums: Vec<i32>) -> i32 {
38+
Self::max_frequency_elements(nums)
39+
}
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
#[test]
45+
fn test_solution() {
46+
super::super::tests::run::<super::Solution>();
47+
}
48+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn max_frequency_elements(nums: Vec<i32>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [(&[1, 2, 2, 3, 1, 4] as &[_], 4), (&[1, 2, 3, 4, 5], 5)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::max_frequency_elements(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)