20241228 finished.
This commit is contained in:
parent
f0bd50fbb3
commit
c51c93a371
|
@ -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 p3083_existence_of_a_substring_in_a_string_and_its_reverse;
|
||||||
|
|
||||||
mod p3159_find_occurrences_of_an_element_in_an_array;
|
mod p3159_find_occurrences_of_an_element_in_an_array;
|
||||||
|
|
||||||
|
mod p3046_split_the_array;
|
||||||
|
|
42
src/problem/p3046_split_the_array.rs
Normal file
42
src/problem/p3046_split_the_array.rs
Normal 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]));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user