This is an interesting problem that is described as being NP Complete, meaning it is a problem for which it is difficult to find a solution. The problem can be stated as follows:
Given $m$ objects with different volumes and given containers that each holds the same volume $v$, what is the minimum number $n$ of bins required to hold the given objects. Of course, we that each object has a volume less than or equal to the volume of the bins.
We will not ask you to find the best (or optimal) solution. Instead, we will ask you to devise an algorithm that simply returns a number of bins. Your goal will be for your algorithm to return a solution that comes as close as possible to the optimal solution.
Input: the volume $v$ of each bin, and the volumes of each of the
$m$ objects.
Output: the minimum number $n$ of bins required and which objects
each of the $n$ bins contains.
Given $m$ objects with different volumes and given containers ("bins") that hold a fixed volume $v$, what is the minimum number of bins required to hold the given objects?
The fastest solution can be actually described very easily: Given $m$ objects, these $m$ objects can be stored in $m$ bins, and the $k$th bin contains the $k$th object. This is not, however, a very satisfying solution.
Here is a potentially better solution:
Number the bins available from $1$, and then
Again, however, this is not very satisfying, for if the volume of the bins are $4$ and there are four objects of volume $2$, $3$, $1$, $2$, if you were to following this algorithm, the first bin would contain the items with volumes $2$ and $1$, the second bin would contain the item with volume $3$, and this would require a third bin containing the other object with volume $2$. Can you do better?
You can read more about this problem at Wikipedia and Steven Skiena's algorithm repository.
If you're interested, the algorithm that that finds the optimal solution is such that if it takes one minute to find the solution for $m$ objects, it takes more than two minutes to find the solution for $m + 1$ objects.
A slightly different question is:
Input: the volume $v$ of each bin, the number of bins $K$, and
the volumes of each of the $m$ objects.
Output: either 'yes' or 'no', and if 'yes', a list of which objects go into
which bins.
Given $m$ objects with different volumes and given $K$ containers ("bins") that hold a fixed volume $v$, can the $m$ objects be distributed between the $K$ containers?