Problem Description
Given a sentence (a string of words separated by single spaces) and a maximum row length k, split the sentence into rows such that no row exceeds k characters. You cannot break a word between rows and must preserve word order. Every row (except the last) incurs a cost of (k - row_length)². The task is to determine the minimum possible total cost for such a split.
Key Insights
- The problem is a variant of the "Word Wrap" problem where the cost for the last line is zero.
- Use dynamic programming (DP) where dp[i] represents the minimum cost to split the sentence starting from the i-th word.
- For each starting word index i, iterate over possible ending words j to form a valid row considering the extra spaces between words.
- If a valid row is formed (row length ≤ k), compute:
- If j is not the last word, add the cost (k - row_length)² plus dp[j+1].
- If j is the last word, the cost is 0 (last line penalty is ignored).
- The answer is dp[0], the minimum cost to arrange all words.
Space and Time Complexity
Time Complexity: O(n²), where n is the number of words. In the worst case, each word is considered with every possible ending word. Space Complexity: O(n) for storing the dp array.
Solution
We solve the problem using dynamic programming. First, split the sentence into words. Define dp[i] as the minimum cost to format the sentence from the i-th word to the end.
For each index i (starting from the last word moving backwards), try to add words one-by-one to form the current row, keeping track of the current row length (which includes words and the spaces between them). If the current row length exceeds k, break out of the inner loop. Otherwise, if j is not the last word, calculate the cost as (k - current_length)² and add dp[j + 1]. If j is the last word, there is no cost for this row (cost = 0). The minimum of these options gives dp[i].
We use iterative DP (bottom-up approach) to ensure that all subproblems are calculated before being used in the recurrence.