Leetcode Study Day 12

Text Justification

Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ‘ ‘ when necessary so that each line has exactly maxWidth characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left-justified, and no extra space is inserted between words.

Note:

A word is defined as a character sequence consisting of non-space characters only.
Each word’s length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Example 1:

Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]
Example 2:

Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified because it contains only one word.
Example 3:

Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]


Constraints:

1 <= words.length <= 300
1 <= words[i].length <= 20
words[i] consists of only English letters and symbols.
1 <= maxWidth <= 100
words[i].length <= maxWidth

Solution

I didn’t find the solution by my own, so this is a good solution from the community with my understanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
// the index where the current line starts.
int start = 0;

while (start <words.size()) {
// Helps to find the last word that can fit in the current line.
int end = start;
// The length of the current line if words were just joined without extra spaces
int lineLength = words[end].length();

// The inner while loop checks if the next word can fit on the current line.
//If it fits, end is incremented, and the length of the next word is added to lineLength.
// The condition (end + 1 - start) calculates the minimum number of spaces required (one space for each word except the last one).

while (end + 1 < words.size() && lineLength + words[end + 1].length() + (end + 1 - start) <= maxWidth) {
++end;
lineLength += words[end].length();
}

string line = words[start];
int numWords = end - start;

if (end == words.size() - 1 || numWords == 0) { // Left justify for last line or single word line
for (int i = start + 1; i <= end; ++i) {
line += " " + words[i];
}
line += string(maxWidth - line.length(), ' ');
} else {
// If not the last line
// Calculate total spaces that need to be added to this line.
int totalSpaces = maxWidth - lineLength;
// Determine the average spaces between each word
int spacesBetweenWords = totalSpaces / numWords;
// Calculate extra spaces that cannot be evenly divided among words
int extraSpaces = totalSpaces % numWords;

for (int i = start + 1; i <= end; ++i) {
// Determine the number of spaces before this word (spacesBetweenWords + 1 if there are extra spaces left, spacesBetweenWords otherwise).
int spaces = spacesBetweenWords + (extraSpaces-- > 0 ? 1 : 0);
line += string(spaces, ' ') + words[i];
}
}
// After constructing the current line, push it to the result.
// Move start to end + 1 for processing the next line.
result.push_back(line);
start = end + 1;
}

return result;
}
};
  • Copyrights © 2020-2024 Yangyang Cui

请我喝杯咖啡吧~

支付宝
微信