-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0068-text-justification.py
More file actions
51 lines (45 loc) · 1.76 KB
/
0068-text-justification.py
File metadata and controls
51 lines (45 loc) · 1.76 KB
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
# time complexity: O(n*k)
# space complexity: O(m)
from typing import List
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
def getWords(i):
currentLine = []
currLength = 0
while i < len(words) and currLength + len(words[i]) <= maxWidth:
currentLine.append(words[i])
currLength += len(words[i]) + 1
i += 1
return currentLine
def createLine(line, i):
baseLength = -1
for word in line:
baseLength += len(word) + 1
extraSpaces = maxWidth - baseLength
if len(line) == 1 or i == len(words):
return " ".join(line) + " " * extraSpaces
wordCount = len(line) - 1
spacesPerWord = extraSpaces // wordCount
needsExtraSpace = extraSpaces % wordCount
for j in range(needsExtraSpace):
line[j] += " "
for j in range(wordCount):
line[j] += " " * spacesPerWord
return " ".join(line)
result = []
i = 0
while i < len(words):
currentLine = getWords(i)
i += len(currentLine)
result.append(createLine(currentLine, i))
return result
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
print(Solution().fullJustify(words, maxWidth))
words = ["What", "must", "be", "acknowledgment", "shall", "be"]
maxWidth = 16
print(Solution().fullJustify(words, maxWidth))
words = ["Science", "is", "what", "we", "understand", "well", "enough", "to",
"explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"]
maxWidth = 20
print(Solution().fullJustify(words, maxWidth))