20250319 finished.

This commit is contained in:
jackfiled 2025-03-19 12:33:29 +08:00
parent c0a526d745
commit 0d6b61e92c
2 changed files with 49 additions and 0 deletions

View File

@ -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;

View File

@ -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<i32>) -> Vec<Vec<i32>> {
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])
);
}
}