Pack N identical objects into distinct containers while minimizing number of containers and empty space

203 Views Asked by At

Suppose you are given N objects that are identical for the purpose of our packing (i.e. there are no preferences based on weight, color, size, etc.). You must sort these into containers, but you only have certain sized containers available and each size can hold a specified number of items. The object is to minimize both the number of containers used (you can have as many as you need of any size) as well as the amount of remaining space.

For example, let the containers have sizes 11, 8, 5, and 3.
For 14 items, you simply use one 11 and one 3.
For 12 items, you could either do 11+3 (remainder 2) or 8+5 (remainder 1). Since both require 2 containers and 8+5 has less empty space, it is better.

EDIT: For 15 items, you could choose 5+5+5 (remainder 0) or 11+5 (remainder 1). I'm not really sure which is better, but for the sake of the question lets say number of containers takes precedence over empty space (i.e. 11+5 wins)


Is there an algorithm that can compute the number of each container size given an array of sizes and a total number of items?


Note that I have searched around but found nothing related to what I am asking. I seem to only be able to find people talking about the knapsack problem or the bin-packing problem. This may be solvable with the knapsack problem and I am just missing something. It also is the reverse of the bin-packing problem with an added constraint of minimizing left over space.

I also am not really sure how to do a better search for answers to this, so if the solution is already out there, please point me in the right direction. Thanks!