
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.
1def fullJustify(words, maxWidth):
2 def justify_line(line, width, maxWidth):
3 if len(line) == 1:
4 return line[0] + ' ' * (maxWidth - width)
5 total_spaces = maxWidth - width + len(line) - 1
6 space, extra = divmod(total_spaces, len(line) - 1)
7 for i in range(extra):
8 line[i] += ' '
9 return (' ' * space).join(line)
10
11 res, line, width = [], [], 0
12 for word in words:
13 if width + len(word) + len(line) > maxWidth:
14 res.append(justify_line(line, width, maxWidth))
15 line, width = [], 0
16 line.append(word)
17 width += len(word)
18
19 last_line = ' '.join(line)
20 res.append(last_line + ' ' * (maxWidth - len(last_line)))
21 return resThe code defines a helper function, justify_line, to construct each justified line. While adding words to the current line, if adding another word exceeds maxWidth, the line is justified and appended to the result. The last line is treated separately for left-justification.
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.
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5public class Solution {
6 public IList<string> FullJustify(string[] words, int maxWidth) {
int n = words.Length;
int[] dp = new int[n + 1];
int[] path = new int[n];
Array.Fill(dp, int.MaxValue);
dp[n] = 0;
for (int i = n - 1; i >= 0; i--) {
int lineLength = -1;
for (int j = i; j < n && lineLength <= maxWidth; j++) {
lineLength += words[j].Length + 1;
if (lineLength - 1 <= maxWidth) {
int lastCost = j == n - 1 ? 0 : maxWidth - (lineLength - 1);
if (dp[i] > dp[j + 1] + lastCost) {
dp[i] = dp[j + 1] + lastCost;
path[i] = j + 1;
}
}
}
}
List<string> result = new List<string>();
int k = 0;
while (k < words.Length) {
int end = path[k];
StringBuilder line = new StringBuilder(words[k]);
for (int i = k + 1; i < end; i++) {
line.Append(' ').Append(words[i]);
}
line.Append(new string(' ', maxWidth - line.Length));
result.Add(line.ToString());
k = end;
}
return result;
}
}This C# implementation also utilizes dynamic programming to find the least-cost configuration of lines based on word inclusion. The resulting justified lines respect the discovered optimal path ensuring the efficient use of space on each line.