-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_operations_to_make_arr_empty_2870.rs
executable file
·45 lines (35 loc) · 1.32 KB
/
min_operations_to_make_arr_empty_2870.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::collections::HashMap;
use crate::Solution;
use std::collections::HashSet;
/* You are given a 0-indexed array nums consisting of positive integers.
There are two types of operations that you can apply on the array any number of times:
Choose two elements with equal values and delete them from the array.
Choose three elements with equal values and delete them from the array.
Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
*/
impl Solution {
pub fn min_operations(nums: Vec<i32>) -> i32 {
let mut count = HashMap::new();
let mut total_operations = 0;
for &num in nums.iter() {
*count.entry(num).or_insert(0) += 1;
}
//NOTE: This solution uses the pigeonhole principle
//pigeonhole principle of divisibility, basically, you want to group things into maximums
//of three, and if they dont fit in 3, you might need just one more operation for that
//extra group of 2, or 1
for &freq in count.values() {
if freq == 1 {
return -1;
}
total_operations += freq / 3;
if freq % 3 != 0 {
total_operations+=1;
}
}
total_operations
}
}
fn main() {
unimplemented!();
}