Skip to content

Commit 5fa33a8

Browse files
committed
Add problem 2870: Minimum Number of Operations to Make Array Empty
1 parent a27a262 commit 5fa33a8

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,7 @@ pub mod problem_2856_minimum_array_length_after_pair_removals;
19971997
pub mod problem_2859_sum_of_values_at_indices_with_k_set_bits;
19981998
pub mod problem_2864_maximum_odd_binary_number;
19991999
pub mod problem_2869_minimum_operations_to_collect_elements;
2000+
pub mod problem_2870_minimum_number_of_operations_to_make_array_empty;
20002001

20012002
#[cfg(test)]
20022003
mod test_utilities;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::collections::HashMap;
6+
7+
impl Solution {
8+
pub fn min_operations(nums: Vec<i32>) -> i32 {
9+
let mut counts = HashMap::<_, u32>::new();
10+
11+
for num in nums {
12+
counts.entry(num).and_modify(|count| *count += 1).or_insert(1);
13+
}
14+
15+
let mut result = 0;
16+
17+
for &value in counts.values() {
18+
if value == 1 {
19+
return -1;
20+
}
21+
22+
result += value.div_ceil(3);
23+
}
24+
25+
result as _
26+
}
27+
}
28+
29+
// ------------------------------------------------------ snip ------------------------------------------------------ //
30+
31+
impl super::Solution for Solution {
32+
fn min_operations(nums: Vec<i32>) -> i32 {
33+
Self::min_operations(nums)
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
#[test]
40+
fn test_solution() {
41+
super::super::tests::run::<super::Solution>();
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod hash_map;
2+
3+
pub trait Solution {
4+
fn min_operations(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 = [(&[2, 3, 3, 2, 2, 4, 2, 3, 4] as &[_], 4), (&[2, 1, 2, 2, 3, 3], -1)];
13+
14+
for (nums, expected) in test_cases {
15+
assert_eq!(S::min_operations(nums.to_vec()), expected);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)