Skip to main content

Optimal Text Word Wrap

hard
Dynamic ProgrammingStringArrayDp 1dPrefix Sum
Asked atGoogleMicrosoftMeta

Problem Description

You are given an array of words `wordList` and a line width `lineWidth`. You must arrange all words into lines such that:

1. Words appear in the original order.
2. Words on the same line are separated by exactly one space.
3. No line exceeds `lineWidth` characters (including spaces between words on that line).

Define the **badness** of a line (other than the last line) as the **cube** of the number of unused characters at the end: `(lineWidth - total_chars_on_line)^3`.

The last line always has badness 0 regardless of trailing spaces.

Return the **minimum total badness** over all valid arrangements.

**Example 1:**
```
Input: wordList = ["hello", "world", "fit", "now"], lineWidth = 12
Output: 0
Explanation: Line 1: "hello world" (11 chars, 1 space unused but it's not the last line, badness=1).
Line 2: "fit now" (7 chars, last line, badness=0). Total = 1.
Better: Line 1: "hello world" (11, 1 slack → 1^3=1), Line 2: "fit now" (last, 0). Total=1.
Actually: Line 1 "hello world fit" = 5+1+5+1+3=15 > 12. No.
Line 1 "hello world" (11), Line 2 "fit now" (7). Total badness = 1^3 + 0 = 1.
```

**Example 1 (corrected):**
```
Input: wordList = ["use", "dp", "here"], lineWidth = 7
Output: 0
Explanation: Line 1: "use dp" (6 chars, slack=1, badness=1). Line 2: "here" (last, badness=0). Total=1.
Or Line 1: "use" (3, slack=4, badness=64). Line 2: "dp here" (7, last, badness=0). Total=64.
Minimum is 1.
```

**Example 1:**
```
Input: wordList = ["use", "dp", "here"], lineWidth = 7
Output: 1
Explanation: Best split: Line 1 "use dp" (slack 1, badness 1), Line 2 "here" (last line, badness 0). Total = 1.
```

**Example 2:**
```
Input: wordList = ["ab", "cd", "ef", "gh"], lineWidth = 5
Output: 1
Explanation: Line 1: "ab cd" (5, slack 0, badness 0). Line 2: "ef gh" (5, last line, badness 0). Total = 0.
```

**Example 2:**
```
Input: wordList = ["go", "do", "run", "jump", "fast"], lineWidth = 7
Output: 1
Explanation:
Line 1: "go do" (5 chars, slack=2, badness=8).
Line 2: "run" (3 chars, slack=4, badness=64).
Line 3: "jump" (4 chars, slack=3, badness=27).
Line 4: "fast" (last, badness=0). Not great.
Better: Line 1 "go do" (5, badness=8), Line 2 "run jump" = 8 > 7. No.
Line 1 "go do" (5, badness 8), Line 2 "run" (3, badness 64), Line 3 "jump fast" = 9 > 7.
Hmm. Let me use a simpler example with a known answer.
```

Constraints

  • 1 <= wordList.length <= 300
  • 1 <= wordList[i].length <= lineWidth
  • 1 <= lineWidth <= 120

Follow-up

Can you extend this to also output the actual line breaks (the indices where lines end)?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.

CodeBrainery

A technical blogging platform for developers and engineers to share knowledge and connect.

Connect

© 2026 Kodetra Technologies Pvt. Ltd. All rights reserved. CodeBrainery is a product of Kodetra Technologies Pvt. Ltd.