Skip to content

Commit

Permalink
Add problem 1889: Minimum Space Wasted From Packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed May 16, 2024
1 parent 40dc61f commit 55c3689
Show file tree
Hide file tree
Showing 3 changed files with 106 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 @@ -1407,6 +1407,7 @@ pub mod problem_1884_egg_drop_with_2_eggs_and_n_floors;
pub mod problem_1886_determine_whether_matrix_can_be_obtained_by_rotation;
pub mod problem_1887_reduction_operations_to_make_the_array_elements_equal;
pub mod problem_1888_minimum_number_of_flips_to_make_the_binary_string_alternating;
pub mod problem_1889_minimum_space_wasted_from_packaging;
pub mod problem_1893_check_if_all_the_integers_in_a_range_are_covered;
pub mod problem_1894_find_the_student_that_will_replace_the_chalk;
pub mod problem_1897_redistribute_characters_to_make_all_strings_equal;
Expand Down
25 changes: 25 additions & 0 deletions src/problem_1889_minimum_space_wasted_from_packaging/mod.rs
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 src/problem_1889_minimum_space_wasted_from_packaging/prefix_sum.rs
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(&current_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>();
}
}

0 comments on commit 55c3689

Please sign in to comment.