-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2363: Merge Similar Items
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} | ||
} |