I recently had an interview question as follows: We are given a list of words, and we want to format them to maximize the # of carriage returns while keeping the # of letters per line within a range.
For example, we want a range of 5 - 10 (inclusive) letters per line, one solution is this:
hello (5) cat (3)
Another is this:
hello cat (9) <- 1 for a space
So the first solution is better because we have 1 carriage return versus 0 in the second.
If a word does not fit, it must be placed on a new line. For example:
hello (5) people (6)
Intuitively to me this seems like a greedy algorithm problem where we return as soon as we meet the minimum letter per line constraint. However, this seems too simple and I'm now starting to doubt myself, but I can't come up with a counter example where greedy is not the best.