-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_068_TextJustification.py
More file actions
36 lines (28 loc) · 1.27 KB
/
_068_TextJustification.py
File metadata and controls
36 lines (28 loc) · 1.27 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
#-----------------------------------------------------------------------------
# Runtime: 32ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def fullJustify(self, words: [str], maxWidth: int) -> [str]:
result = []
start_index, current_length = 0, 0
for i in range(len(words)):
if current_length + len(words[i]) > maxWidth:
space = maxWidth - current_length + (i - start_index)
new_line = ''
for j in range(start_index, i):
new_line += words[j]
space_count = maxWidth - len(new_line) if j == i - 1 else space // (i - start_index - 1) + (1 if (j - start_index < (space % (i - start_index - 1))) else 0)
new_line += ' ' * space_count
result.append(new_line)
current_length = 0
start_index = i
current_length += len(words[i]) + 1
new_line = ''
for j in range(start_index, len(words)):
new_line += words[j]
space_count = 1 if j != len(words) - 1 else maxWidth - len(new_line)
new_line += ' ' * space_count
result.append(new_line)
return result