3

I have two instances where I'd like to display information in a "justified" alignment - but I don't care if the values are switched in order. One example being displaying the usernames of people online:

Anton Brother68 Commissar Dougheater Elflord Foobar Goop Hoo Iee Joo

Rearranging them we could get exactly 22 characters long on each line:

Anton Brother68 Foobar
Commissar Elflord Goop
Dougheater Hoo Iee Joo

This is kind of a knapsack, except seems like there ought to be a P solution since I don't care about perfection, and I have multiple lines.

Second instance is identical, except instead of names and character count I would be displaying random images and use their width.

Mikhail
  • 367

2 Answers2

1

It doesn't feel like a knapsack problem because the utlity of all words placed is 1 (correct me if I'm wrong).

Perhaps binpacking instead?

Jonno
  • 1,738
  • 10
  • 17
1

Decide the number of lines, for example by taking the total length and dividing by some maximum line size. Sort the list of names by descending length. Repeatedly take the longest name off the sorted list and add it to the shortest line so far. After you have added all names to the lines, you may want to randomize the order or sort alphabetically within the lines to make it look better.

This is a very close approximation of the optimum solution.