This is called the basket packing problem (this is NP-hard).
Just sorting the descending order by their size, and then inserting each element in the first bit in the list with sufficient remaining space, we get 11/9 OPT + 6/9 buffers (where OPT is the number of bins used in the optimal solution). This can easily take O(n²) or perhaps O(n log n) with an efficient implementation.
In terms of optimal solutions, there is no dynamic programming solution, which is also known as a backpack problem. This resource has one option - the main idea:
D[{set}] = the minimum number of bags using each of the items in {set} Then: D[{set1}] = the minimum of all D[{set1} - {set2}] where set2 fits into 1 bag and is a subset of set1
The array index above is literally a collection ā think of it as a value set map, bitmap, or multidimensional array, where each index is 1 or 0 to indicate whether we include an element that matches this size or not.
A linked resource is actually considering several types that can occur several times - I deduced the above solution from this.
The operating time will greatly depend on the number of elements that can fit into the bag - this will be O(minimumBagsUsed.2 maxItemsPerBag ) .
In the case of 1 bag, this is essentially the problem of the sum of the subset . To do this, you can consider the weight as the same as the value and solve using the knapsack algorithm, but this does not work very well for several packages.
Why not? Consider elements 5,5,5,9,9,9 with bag size 16 . If you simply decide the sum of the subset, you will be left with 5,5,5 in one bag and 9 in one bag each (total bag 4 ), and not 5,9 in each of the three packages.
The total amount / backpack is already a difficult problem - if you do not use it for an optimal solution, you can use the sorting / greed method above.