I have a list of rectangles with different sizes.
rects = [100x20, 30x10, 10x10, 70x20, 40x30, 50x10]
I am trying to display a table of these rectangles. If I had a fixed number of columns, I would simply calculate the number of rows and the size of each row and column as follows:
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 with the maximum row width. The number of columns depends on the calculation of the optimal row width at runtime.
With the list above and the maximum row with parameter 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 of ββgetting closer to the situation is to cache the maximum column width for each possible number of columns. Then the last record with row width = max 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 a max record for every possible column number. The list may become quite large. Does anyone know an algorithm to solve this optimization problem?