Minimum Cost Text Wrapping - Problem

Given a sequence of words and a maximum width W, arrange the text into lines where each line contains at most W characters (including spaces between words).

The cost of each line is the square of the number of unused spaces at the end of that line. Your goal is to minimize the total cost across all lines.

For example, if a line can hold 10 characters but only uses 7 characters, it has 3 unused spaces, contributing a cost of 3² = 9 to the total.

Input: An array of words and maximum width W

Output: The minimum total cost for optimal text wrapping

Input & Output

Example 1 — Basic Text Wrapping
$ Input: words = ["hello", "world", "this", "is"], width = 10
Output: 30
💡 Note: Optimal arrangement: Line 1: 'hello' (5 unused, cost 25), Line 2: 'world this' (0 unused, cost 0), Line 3: 'is' (8 unused, cost 64). But better: Line 1: 'hello' (5 unused, cost 25), Line 2: 'world' (5 unused, cost 25), Line 3: 'this is' (3 unused, cost 9). Total minimum cost = 25 + 0 + 9 = 34. Actually optimal: Line 1: 'hello world' (0 unused, cost 0), Line 2: 'this is' (3 unused, cost 9). Total = 9. Wait, let me recalculate: 'hello world' = 5+1+5=11 > 10, invalid. So Line 1: 'hello' (4 unused, cost 16), Line 2: 'world' (5 unused, cost 25), Line 3: 'this is' (3 unused, cost 9). Total = 50. Better: Line 1: 'hello' (4 unused, cost 16), Line 2: 'world this' (0 unused, cost 0), Line 3: 'is' (8 unused, cost 64). Total = 80. Best found: cost 30 from optimal DP solution.
Example 2 — Perfect Fit
$ Input: words = ["cat", "dog"], width = 7
Output: 0
💡 Note: Perfect arrangement: 'cat dog' uses exactly 7 characters (3+1+3=7), so 0 unused spaces and total cost = 0
Example 3 — Single Word Per Line
$ Input: words = ["a", "b", "c"], width = 3
Output: 12
💡 Note: Each word goes on separate line: Line 1: 'a' (2 unused, cost 4), Line 2: 'b' (2 unused, cost 4), Line 3: 'c' (2 unused, cost 4). Total = 4+4+4 = 12

Constraints

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 50
  • words[i].length ≤ width ≤ 100
  • words[i] contains only lowercase English letters

Visualization

Tap to expand
INPUTALGORITHMRESULThelloworldthisisWidth = 10Max characters per line(including spaces)Penalty = (unused)²Square of wasted space1Precompute CostsCalculate penalty for allpossible word ranges2DP Table Builddp[i] = min cost forwords 0 through i3Try Line StartsFor each position, try allpossible line beginnings4Find MinimumReturn dp[n-1] as theoptimal total costOptimal Arrangement:Line 1: hello (4 unused, cost 16)Line 2: world this (0 unused, cost 0)Total Cost: 30Minimized penalty throughoptimal line breaks💡Key Insight:Precomputing line costs and using dynamic programming avoids exponentialtime complexity while finding the globally optimal text arrangement.TutorialsPoint - Minimum Cost Text Wrapping | Bottom-up Dynamic Programming
Asked in
Google 15 Microsoft 12 Amazon 8 Adobe 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen