From 0d6b61e92cd3679742e37c4a54122d50cdf2a419 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 19 Mar 2025 12:33:29 +0800 Subject: [PATCH] 20250319 finished. --- src/problem/mod.rs | 2 + ...n_array_into_a_2d_array_with_conditions.rs | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/problem/p2610_convert_an_array_into_a_2d_array_with_conditions.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4122396..a25aa92 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -546,3 +546,5 @@ mod p2272_substring_with_largest_variance; mod p1963_minimum_number_of_swaps_to_make_the_string_balanced; mod p2614_prime_in_diagonal; + +mod p2610_convert_an_array_into_a_2d_array_with_conditions; diff --git a/src/problem/p2610_convert_an_array_into_a_2d_array_with_conditions.rs b/src/problem/p2610_convert_an_array_into_a_2d_array_with_conditions.rs new file mode 100644 index 0000000..0d9ef6f --- /dev/null +++ b/src/problem/p2610_convert_an_array_into_a_2d_array_with_conditions.rs @@ -0,0 +1,47 @@ +/** + * [2610] Convert an Array Into a 2D Array With Conditions + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn find_matrix(nums: Vec) -> Vec> { + let mut result = vec![]; + let mut map = HashMap::new(); + + for i in nums { + let entry = map.entry(i).or_insert(0); + *entry += 1; + } + + while !map.is_empty() { + let mut row = Vec::with_capacity(map.len()); + for (k, v) in map.iter_mut() { + row.push(*k); + *v -= 1; + } + + map.retain(|_, &mut x| x != 0); + result.push(row); + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2610() { + assert_eq!( + vec![vec![1, 3, 4, 2], vec![1, 3], vec![1]], + Solution::find_matrix(vec![1, 3, 4, 1, 2, 3, 1]) + ); + } +}