Simplified knapsack in PHP

Posted by Mikhail on Programmers See other posts from Programmers or by Mikhail
Published on 2011-07-07T15:25:20Z Indexed on 2011/11/14 18:11 UTC
Read the original article Hit count: 206

Filed under:

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.

© Programmers or respective owner

Related posts about algorithms

Related posts about np-complete