You are on page 1of 1

Optimise packing balls in a box

Problem:
Balls arrive in a crate in packs of 1,2,3, and 4
These balls must be packed in a smaller box to be sent to the store.
each box can hold up to 6 balls.
Calculate how many boxes are needed to pack the balls.
You are not allowed to split the packs
your test case is
(1,3,2,4,1,1,2)
the desired result should be:
use 3 boxes and waste 4 slots
the human approach is to sort the list to (1,1,1,2,2,3,4)
counting along the list until the running count of balls is going to exceed the number of balls a box
can hold
add one box
repeat
this yields
Box 1 (5,1) = 1,1,1,2 -> look ahead and see the next 2 will exceed the box limit add one box
Box 2 (5,1)= 2,3 -> look ahead and see the next 4 will exceed the box limit
box 3 (4,2) = 4 -> reached end of crate

You might also like