#1564
Medium Algorithms Put boxes into the warehouse i
Array Greedy Sorting
67.4% acceptance
Mar 31, 2026
344
30
No description available.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn max_boxes_in_warehouse(mut boxes: Vec<i32>, warehouse: Vec<i32>) -> i32 {
boxes.sort_unstable();
// Precompute effective heights: each room is limited by min height to its left
let mut effective = warehouse.clone();
for i in 1..effective.len() {
effective[i] = effective[i].min(effective[i - 1]);
}
// Greedily place smallest boxes in rightmost available positions
let mut count = 0;
let mut bi = 0;
for i in (0..effective.len()).rev() {
if bi >= boxes.len() { break; }
if boxes[bi] <= effective[i] {
count += 1;
bi += 1;
}
}
count
}
}