-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0068H. Text Justification.py
More file actions
36 lines (35 loc) · 1.28 KB
/
0068H. Text Justification.py
File metadata and controls
36 lines (35 loc) · 1.28 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: 28 ms, faster than 85.95% of Python3 online submissions for Text Justification.
#Memory Usage: 14.2 MB, less than 93.86% of Python3 online submissions for Text Justification.
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
cand = []
temp = ''
re = ''
w_n = 0
w_l = 0
for i in words:
if len(temp) + len(i) > maxWidth:
if w_n-1 == 0:
temp = temp[:-1]
temp += ' '*(maxWidth-len(temp))
cand.append(temp)
else:
all_n = (maxWidth-w_l)//(w_n-1)
part_n = (maxWidth-w_l)%(w_n-1)
part = temp.split(' ')
re = part[0]
for j in range(part_n):
re += ' '*(all_n+1) + part[j+1]
for j in range(w_n - 1 - part_n):
re += ' '*(all_n) + part[part_n + j + 1]
cand.append(re)
temp = ''
w_n = 0
w_l = 0
temp += i + " "
w_n += 1
w_l += len(i)
temp = temp[:-1]
temp += " "*(maxWidth-len(temp))
cand.append(temp)
return cand