100-days-of-rust/Week-02/Day-13_Need-Help-With-Packing/day13/src/lib.rs

29 lines
763 B
Rust

use std::collections::VecDeque;
pub fn can_fit(weights: &[usize], bags: usize) -> bool {
bags >= min_bags(&mut VecDeque::from(weights.to_vec()), 0)
}
fn min_bags(weights: &mut VecDeque<usize>, current_weight: usize) -> usize {
if weights.is_empty() {
return if current_weight > 0 { 1 } else { 0 };
}
let mut min = usize::MAX;
for _ in 0..weights.len() {
let w = match weights.pop_front() {
Some(x) => x,
None => panic!("At the disco"),
};
if current_weight + w > 10 {
min = std::cmp::min(min, min_bags(weights, w)) + 1;
} else {
min = std::cmp::min(min, min_bags(weights, current_weight + w));
}
weights.push_back(w);
}
min
}