text (words of lowercase English letters separated by single spaces) and a line width width. Break the text into rows by inserting line breaks between words:
- words cannot be split or reordered;
- neighbouring words on a row are separated by exactly one space, with no leading or trailing spaces;
- every row is at most width characters long.
A row of length len has a penalty of (width − len)². The penalty of the whole layout is the sum over all rows **except the last one**.
Return the smallest possible penalty.text="we ship code daily", width=8 → 17
The best split is "we ship" (7 characters, penalty (8 − 7)² = 1), then "code" ((8 − 4)² = 16), and the last row "daily" is free. Total 17.
text="tiny note", width=20 → 0
The whole text fits on one row, and the last row carries no penalty.
text="aa bb cc", width=5 → 0
text consists of lowercase English letters and spaces, does not start or end with a space, and words are separated by single spaceswidthtext="we ship code daily", width=8
17
Run checks the first 3 tests in your browser. Submit checks all 85 tests on the server, hidden ones included.