-
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 1889: Minimum Space Wasted From Packaging
- Loading branch information
Showing
3 changed files
with
106 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
25 changes: 25 additions & 0 deletions
25
src/problem_1889_minimum_space_wasted_from_packaging/mod.rs
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,25 @@ | ||
pub mod prefix_sum; | ||
|
||
pub trait Solution { | ||
fn min_wasted_space(packages: Vec<i32>, boxes: Vec<Vec<i32>>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[2, 3, 5] as &[_], &[&[4, 8] as &[_], &[2, 8] as &[_]] as &[&[_]]), 6), | ||
((&[2, 3, 5], &[&[1, 4], &[2, 3], &[3, 4]]), -1), | ||
((&[3, 5, 8, 10, 11, 12], &[&[12], &[11, 9], &[10, 5, 14]]), 9), | ||
]; | ||
|
||
for ((packages, boxes), expected) in test_cases { | ||
assert_eq!( | ||
S::min_wasted_space(packages.to_vec(), boxes.iter().copied().map(<[_]>::to_vec).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/problem_1889_minimum_space_wasted_from_packaging/prefix_sum.rs
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,80 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn sort_as_u32(values: Vec<i32>) -> Vec<u32> { | ||
let mut values = values.into_iter().map(|x| x as u32).collect::<Vec<_>>(); | ||
|
||
values.sort_unstable(); | ||
|
||
values | ||
} | ||
|
||
pub fn min_wasted_space(packages: Vec<i32>, boxes: Vec<Vec<i32>>) -> i32 { | ||
let max_package = packages.iter().fold(0, |max, &size| max.max(size as _)); | ||
let mut sums = vec![0_u64; max_package as usize + 1].into_boxed_slice(); | ||
let mut required_space = 0; | ||
|
||
for &package in &packages { | ||
let package = package as u32; | ||
|
||
required_space += u64::from(package); | ||
sums[package as usize] += 1; | ||
} | ||
|
||
let mut sum = 0; | ||
|
||
for target in &mut *sums { | ||
sum += *target; | ||
*target = sum; | ||
} | ||
|
||
let mut result = u64::MAX; | ||
|
||
for boxes in boxes { | ||
let boxes = Self::sort_as_u32(boxes); | ||
|
||
if boxes.last().map_or(false, |&box_size| box_size >= max_package) { | ||
let mut used = 0; | ||
let mut prev_count = 0; | ||
|
||
for size in boxes { | ||
if let Some(¤t_count) = sums.get(size as usize) { | ||
used += u64::from(size) * (current_count - prev_count); | ||
prev_count = current_count; | ||
} else { | ||
used += u64::from(size) * (sum - prev_count); | ||
|
||
break; | ||
} | ||
} | ||
|
||
result = result.min(used); | ||
} | ||
} | ||
|
||
if result != u64::MAX { | ||
result -= required_space; | ||
result %= 1_000_000_007; | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_wasted_space(packages: Vec<i32>, boxes: Vec<Vec<i32>>) -> i32 { | ||
Self::min_wasted_space(packages, boxes) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |