Minimum number of boxes we need to pack the items

1.5k Views Asked by At

We have $N$ items of the same size and we want to them in boxes. We have boxes of different sizes $\{M_1,\ldots , M_k\}$, how many we want from each size. Each box has be fully filled.

Design an algorithm that calculates the minimum number of boxes we need to pack the items.

We consider $M_1=1$ to have always fully boxes independent of $N$.

$$$$

It looks like Knapsack problem. So do we have to apply the algorithm of Knapsack problem for each box?

So we could sort the boxes in increasing order as for the size.

Then we starting packing items in that box, if it gets full we go to the next box.

We do that till no items are left.

Is the idea correct?

1

There are 1 best solutions below

1
On BEST ANSWER

We indeed can solve it as Knapsack problem - for each $n \leq N$ calculate $a_n$ - minimum number of boxes we need for $n$ items by recurrent $$a_n = \begin{cases} \infty, & n < 0 \\ 0, & n = 0 \\ 1 + \min_i a_{n - M_i}, & n > 0 \end{cases}$$

Greedy algorithm doesn't work, as usual: let $N = 10$, $M_2 = 6$, $M_3 = 5$. Then greedy algorithm uses one box of size $6$ and $4$ boxes of size $1$ for a total of $5$ boxes, while optimal solution is to use two boxes of size $5$.