A canvas-based text editor lays out a paragraph whose word lengths are wordLengths, in order, into lines at most width characters wide. Words on the same line are separated by one space, and words can't be split or reordered.
A line with extra unused characters at its end costs extra * extra, except the last line, which is free. Write leastRaggedCost(wordLengths, width) that returns the smallest total cost of a layout. Every word fits on a line by itself.
Input: wordLengths = [3,2,2,5], width = 6 Output: 10
Lines of lengths 3 (3 extra), 2 + 1 + 2 = 5 (1 extra), and the last line 5: 9 + 1 = 10. Filling the first line greedily (3 + 1 + 2 = 6) leaves a line of just 2 and costs 16.
Input: wordLengths = [4], width = 4 Output: 0
A single line is the last line, which is free.
1 <= wordLengths.length <= 5001 <= wordLengths[i] <= width <= 100Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #550 Wrap Text With the Least Ragged Edge. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.