dsa · hard
Text Justification
Pack words into lines of exactly max_width characters, fully justified.
Arguments
words— words to pack, in order; none contains a spacemax_width— exact character width of every returned line
Greedy: put as many words on a line as will fit with at least one space between neighbors. Extra spaces on a packed line are spread across the gaps as evenly as possible; leftover spaces go to the **leftmost** gaps. A line with a single word is left-aligned and padded with spaces on the right. The **last** line is left-aligned (one space between words, spaces on the right to reach max_width). Every returned line has length max_width.
Example
words = ["This","is","an","example","of","text","justification."], max_width = 16 packs three lines:
This is an — words This/is/an use 8 letters, 8 extra spaces split 4 and 4.
example of text — 13 letters, 3 extra spaces become 2 then 1.
justification. — last line, left-aligned, two trailing spaces.
Constraints
1 <= words.length <= 300 1 <= words[i].length <= 20 1 <= max_width <= 100 words[i].length <= max_width words[i] has no spaces Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: ["This","is","an","example","of","text","justification."] 16 Expected: ["This is an","example of text","justification. "]
Example 2
Input: ["What","must","be","acknowledgment","shall","be"] 16 Expected: ["What must be","acknowledgment ","shall be "]