Skip to content

Commit

Permalink
Add problem 2363: Merge Similar Items
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 15, 2024
1 parent dd8b9db commit 8aed493
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ pub mod problem_2353_design_a_food_rating_system;
pub mod problem_2357_make_array_zero_by_subtracting_equal_amounts;
pub mod problem_2358_maximum_number_of_groups_entering_a_competition;
pub mod problem_2360_longest_cycle_in_a_graph;
pub mod problem_2363_merge_similar_items;

#[cfg(test)]
mod test_utilities;
50 changes: 50 additions & 0 deletions src/problem_2363_merge_similar_items/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::mem;

impl Solution {
pub fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut items1 = items1;
let mut items2 = items2;

if items2.len() < items1.len() {
mem::swap(&mut items1, &mut items2);
}

let sort_key = |item: &Vec<_>| item[0] as u32;

items1.sort_unstable_by_key(sort_key);

for item in &mut items2 {
let [value, weight] = <&mut [_; 2]>::try_from(item.as_mut_slice()).ok().unwrap();

if let Ok(i) = items1.binary_search_by(|item| u32::cmp(&(item[0] as _), &(*value as _))) {
*weight += mem::take(&mut items1[i][1]);
}
}

items2.extend(items1.into_iter().filter(|item| item[1] != 0));

items2.sort_unstable_by_key(sort_key);

items2
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
Self::merge_similar_items(items1, items2)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
37 changes: 37 additions & 0 deletions src/problem_2363_merge_similar_items/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub mod iterative;

pub trait Solution {
fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>>;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(
(&[[1, 1], [4, 5], [3, 8]] as &[_], &[[3, 1], [1, 5]] as &[_]),
&[[1, 6], [3, 9], [4, 5]] as &[_],
),
(
(&[[1, 1], [3, 2], [2, 3]], &[[2, 1], [3, 2], [1, 3]]),
&[[1, 4], [2, 4], [3, 4]],
),
(
(&[[1, 3], [2, 2]], &[[7, 1], [2, 2], [1, 4]]),
&[[1, 7], [2, 4], [7, 1]],
),
];

for ((items1, items2), expected) in test_cases {
assert_eq!(
S::merge_similar_items(
items1.iter().map(Vec::from).collect(),
items2.iter().map(Vec::from).collect(),
),
expected,
);
}
}
}

0 comments on commit 8aed493

Please sign in to comment.