← 返回 sofi 的题目列表Basic Text Justification (without full padding to fixed width)
类型:online_judge
Problem: Basic Text Justification (no full padding to fixed width)
Given an array of words words (each word contains only letters, no spaces) and an integer maxWidth, format the text into multiple lines such that:
Each line contains as many words as possible (greedy).
Adjacent words in a line are separated by at least one space.
The length of each line must not exceed maxWidth.
Unlike classic full justification, you do not need to pad spaces to make the line length exactly maxWidth:
No trailing spaces are required.
You do not need to distribute extra spaces evenly between words.
Return all formatted lines in order.
I/O format (for online judge/script)
Input:
Line 1: integer maxWidth
Line 2: integer n (# of words)
Next n lines: one word per line
Output:
Print each formatted line on its own line (no extra trailing spaces)
Constraints
1 <= n <= 1e4
1 <= len(words[i]) <= maxWidth <= 1e4
Total number of characters across all words <= 2e5
Example
maxWidth = 10, words = ["This","is","an","example","text"]
One valid output:
This is an
example
text
Example
Input
10
5
This
is
an
example
text
Output
This is an
example
text