Article Layout
AirbnbPhone screenHard
You are laying out the text of an article as the class ArticleLayout, created with the article's text. The text is words separated by single spaces, and a word is never split across lines.
Part 1: Fit in k Lines
Implement by_line_count(k), which lays the article out in at most k lines and returns them.
Pack greedily at a width W: put words on a line, separated by single spaces, until the next word would make the line longer than W, then start a new line. Use the narrowest W at which this packing needs at most k lines, and return those lines, words joined by single spaces.
W is never less than the longest word, so the result can have fewer than k lines.
- Example 1
- init
["the quick brown fox jumps over the lazy dog"]- operations
[["by_line_count",[3]]]- Output
[["the quick brown","fox jumps over","the lazy dog"]]
Why: At width 14 the greedy lines are
the quick,brown fox,jumps over the,lazy dog, which is four lines. At width 15 they arethe quick brown,fox jumps overandthe lazy dog, which is three, so 15 is the narrowest width and those are the lines.
Constraints
1 <= len(text) <= 10^4, with no leading, trailing or repeated spaces1 <= k <= 10^4