20241228 finished.

This commit is contained in:
jackfiled 2024-12-28 12:12:00 +08:00
parent f0bd50fbb3
commit c51c93a371
2 changed files with 44 additions and 0 deletions

View File

@ -396,3 +396,5 @@ mod p3218_minimum_cost_for_cutting_cake_i;
mod p3083_existence_of_a_substring_in_a_string_and_its_reverse;
mod p3159_find_occurrences_of_an_element_in_an_array;
mod p3046_split_the_array;

View File

@ -0,0 +1,42 @@
/**
* [3046] Split the Array
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashMap;
impl Solution {
pub fn is_possible_to_split(nums: Vec<i32>) -> bool {
let mut map = HashMap::with_capacity(nums.len());
for i in nums {
let entry = map.entry(i).or_insert(0);
*entry += 1;
if *entry > 2 {
return false;
}
}
map.iter()
.filter_map(|(_, v)| if *v == 1 { Some(()) } else { None })
.count()
% 2
== 0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3046() {
assert!(Solution::is_possible_to_split(vec![1, 1, 2, 2, 3, 4]));
assert!(Solution::is_possible_to_split(vec![4, 4, 9, 10]));
assert!(!Solution::is_possible_to_split(vec![1, 1, 1, 1]));
}
}