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, 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 }