
Sponsored
Sponsored
This approach involves iterating through the given words and greedily forming a line by adding as many words as possible without exceeding the maxWidth. When a line is completed, the algorithm distributes spaces evenly between words. If the number of spaces doesn’t divide evenly, more spaces are added to the left slots. The last line is treated differently by left-justifying the text.
Time Complexity: O(n), where n is the total number of characters in all words.
Space Complexity: O(n), where n is the number of lines formed times maxWidth for the justified text.
1import java.util.*;
2
3public class TextJustification {
4 public List<String> fullJustify(String[] words, int maxWidth) {
5 List<String> result = new ArrayList<>();
6 int index = 0;
7 while (index < words.length) {
8 int totalChars = words[index].length();
9 int last = index + 1;
10 while (last < words.length) {
11 if (totalChars + words[last].length() + 1 > maxWidth) break;
12 totalChars += words[last].length() + 1;
13 last++;
14 }
15
16 StringBuilder sb = new StringBuilder();
17 int diff = last - index - 1;
18 // if last line or number of words in the line is 1, left-justify
19 if (last == words.length || diff == 0) {
20 for (int i = index; i < last; i++) {
21 sb.append(words[i]).append(" ");
22 }
23 sb.deleteCharAt(sb.length() - 1);
24 while (sb.length() < maxWidth) {
25 sb.append(" ");
26 }
27 } else {
28 int spaces = (maxWidth - totalChars) / diff;
29 int remainder = (maxWidth - totalChars) % diff;
30 for (int i = index; i < last - 1; i++) {
31 sb.append(words[i]).append(" ");
32 for (int j = 0; j < spaces + (i - index < remainder ? 1 : 0); j++) {
33 sb.append(" ");
34 }
35 }
36 sb.append(words[last - 1]);
37 }
38 result.add(sb.toString());
39 index = last;
40 }
41 return result;
42 }
43}This Java implementation uses a while loop to process words. It determines the range of words that can fit into the current line, then builds the line accordingly. It checks if it's the last line or if only one word exists in the line, in which case left-justification is applied. Otherwise, the spaces are evenly distributed.
This method leverages dynamic programming to minimize a hypothetical cost function that combines badness or uneven space distribution along with total number of lines used, aiming to determine the optimal setup of words per line.
Time Complexity: O(n^2), due to potential double iterations required for exploring word positions.
Space Complexity: O(n), since it uses extra space for storing dp and path arrays.
1#include <iostream>
2#include <vector>
3#include <string>
4#include <climits>
5using namespace std;
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n = words.size();
vector<int> dp(n + 1, INT_MAX);
vector<int> path(n);
dp[n] = 0;
for (int i = n - 1; i >= 0; --i) {
int len = -1;
for (int j = i; j < n; ++j) {
len += words[j].length() + 1;
if (len > maxWidth) break;
int last_cost = j == n - 1 ? 0 : maxWidth - len;
if (dp[i] > last_cost + dp[j + 1]) {
dp[i] = last_cost + dp[j + 1];
path[i] = j + 1;
}
}
}
vector<string> res;
for (int i = 0; i < n; i = path[i]) {
int j = path[i];
string line = words[i];
for (int k = i + 1; k < j; ++k) {
line += " " + words[k];
}
line += string(maxWidth - line.length(), ' ');
res.push_back(line);
}
return res;
}This C++ implementation uses a dynamic programming table, dp, to compute the minimum penalty of breaking words into lines. path is used to store optimal partitioning. The final step reconstructs justified text using information stored in path.