forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText Justification.py
39 lines (29 loc) · 1021 Bytes
/
Text Justification.py
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
from typing import List
class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def fullJustify(self, words: List[str], max_width: int) -> List[str]:
justified_lines = []
width = 0
line = []
for word in words:
if len(word) + width + len(line) <= max_width:
width += len(word)
line.append(word)
continue
if len(line) == 1:
justified_lines.append(line.pop().ljust(max_width))
else:
justified_lines.append(self.format_line(line, width, max_width))
line = [word]
width = len(word)
justified_lines.append(' '.join(line).ljust(max_width))
return justified_lines
@staticmethod
def format_line(line: List[str], width: int, max_width: int) -> str:
space, extra = divmod(max_width - width, len(line) - 1)
for i in range(extra):
line[i] += ' '
return (' ' * space).join(line)