241. Text Justification (Line Count)

MediumStringString

Given an array of words and a maximum width maxWidth, pack words greedily into lines so each line holds as many words as fit (with at least one space between words). Return the number of lines used. Input: '[words], maxWidth'.

Input: '[words], maxWidth'.

Output: Integer — number of lines.

Examples

Example 1
Input: ["This","is","an","example"], 16
Output: 2
Explanation: 'This is an' then 'example'.
Example 2
Input: ["a","b","c","d"], 5
Output: 2
Explanation: 'a b c' then 'd'.
Example 3
Input: ["word"], 10
Output: 1
Explanation: Single line.

Constraints

Asked by

GoogleAppleMetaAmazonMicrosoftBloomberg
Solve this problem in the editor →