I have a list of rectangles with different dimensions.
rects = [100x20, 30x10, 10x10, 70x20, 40x30, 50x10]
I am trying to render a table from these rectangles. If I would have a fix number of columns, I simply could calculate the number of rows and the size of each row and column like this:
numCols = 4;
for (i = 0; i < rects.size - 1, i++):
rect = rects[i];
col = i % numCols;
row = floor(i / numCols);
columns[col] = max(columns[col], rect.width);
rows[row] = max(rows[row], rect.height);
end for;
Now I want my table to be configured by a max row width. The number of columns depends on a runtime calculation of the optimal row width.
With the list above and a max row with set to 140 I expect my table to be:
rects = [100x20, 30x10, 70x10, 10x20, 40x30, 10x10]
100x20, 30x10
70x10, 10x20
40x30, 10x10
cols = [100, 30]
rows = [20, 20, 30]
My first idea to approach the situation is to cache the max column width for each possible number of colums. The last entry with a sum <= max row width then wins.
max[1] = [100]
max[2] = [100, 30] - wins
max[3] = [100, 40, 70] - 210 > 140
max[4] = [100, 30, 70, 10]
max[5] = [100, 30, 70, 10, 40]
max[6] = [100, 30, 70, 10, 40, 10]
Unfortunately, I need to create an entry in max for each possible column number. The list can get pretty big. Does someone know an algorithm to solve this optimization problem?