Minimum Number of Groups / Rows Algorithim

17 Views Asked by At

I am pretty sure this simple problem has a proper name but I have no idea what it is.

I have a number of blocks of different widths (all same height).

I would like to stack these blocks horizontally such that the width of the row does not exceed a predefined Max Width constant. If at any moment placing a new block makes the width of the row exceeds the maximum, the block is removed and put on a new row.

Is there an algorithm for optimal stacking of the blocks such that the number of rows is minimum?

I was able to abstract this problem by saying that I have N different numbers and my objective would be to put these numbers in P groups such that the sum of any group does not exceed M (the maximum). The objective would be to have the minimum number of groups.

What is the name of this problem and how to solve it?