Skip to content

Commit 5e33086

Browse files
committed
070
1 parent 02df3ad commit 5e33086

File tree

2 files changed

+142
-31
lines changed

2 files changed

+142
-31
lines changed

070 Text Justification.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left
3+
and right) justified.
4+
5+
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces
6+
' ' when necessary so that each line has exactly L characters.
7+
8+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide
9+
evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
10+
11+
For the last line of text, it should be left justified and no extra space is inserted between words.
12+
13+
For example,
14+
words: ["This", "is", "an", "example", "of", "text", "justification."]
15+
L: 16.
16+
17+
Return the formatted lines as:
18+
[
19+
"This is an",
20+
"example of text",
21+
"justification. "
22+
]
23+
Note: Each word is guaranteed not to exceed L in length.
24+
25+
click to show corner cases.
26+
27+
Corner Cases:
28+
A line other than the last line might contain only one word. What should you do in this case?
29+
In this case, that line should be left-justified.
30+
"""
31+
__author__ = 'Danyang'
32+
class Solution:
33+
def fullJustify(self, words, L):
34+
"""
35+
36+
:param words: a list of str
37+
:param L: int
38+
:return: a list of str
39+
"""
40+
result = []
41+
self.break_line(words, L, result)
42+
return self.distribute_space(L, result)
43+
44+
def break_line(self, words, L, result):
45+
if not words:
46+
return
47+
48+
cur_length = -1
49+
lst = []
50+
i = 0
51+
while i<len(words):
52+
word = words[i]
53+
cur_length += 1 # space in left justified
54+
cur_length += len(word)
55+
if cur_length>L: break
56+
lst.append(word)
57+
i += 1
58+
59+
result.append(lst)
60+
self.break_line(words[i:], L, result)
61+
62+
63+
def distribute_space(self, L, result):
64+
new_result = []
65+
for ind, line in enumerate(result):
66+
word_cnt = len(line)
67+
str_builder = []
68+
space_cnt = L-sum(len(word) for word in line)
69+
hole_cnt = word_cnt-1
70+
if ind<len(result)-1:
71+
if hole_cnt>0:
72+
space = space_cnt/hole_cnt
73+
remain = space_cnt%hole_cnt
74+
75+
for word in line[:-1]:
76+
str_builder.append(word)
77+
str_builder.append(" "*space)
78+
if remain>0:
79+
str_builder.append(" ")
80+
remain -= 1
81+
82+
str_builder.append(line[-1])
83+
else:
84+
str_builder.append(line[-1])
85+
str_builder.append(" "*space_cnt)
86+
else: # last line, special handling
87+
str_builder = [" ".join(line)]
88+
str_builder.append(" "*(space_cnt-hole_cnt))
89+
90+
new_result.append("".join(str_builder))
91+
92+
return new_result
93+
94+
95+
96+
if __name__=="__main__":
97+
print Solution().fullJustify(["This", "is", "an", "example", "of", "text", "justification."], 16)
98+
print Solution().fullJustify(["What","must","be","shall","be."], 12)

README.md

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
11
# LeetCode
2-
Practice
3-
I am just writing the code for the sake of doing it, since there's lots of fun in learning and writing.
2+
LeetCode is a coding online judger. You may find the problem list [here](https://oj.leetcode.com/problems/).
43

4+
###Battery Included
5+
You can find comprehensive documentation in the source code itself. Documentation includes problem descriptions, algorithms, and test cases.
6+
7+
```
8+
.-==============-. .-==============-.
9+
| BATTERY | | INCLUDED |
10+
(|+ -| (|+ -|
11+
| | | |
12+
'-===============' '-==============-'
13+
```
514
### Problem List
6-
Ranked by chronological order: [Link](http://deepreader.io/LeetCode/problem_list.html)
15+
Indexed by chronological order: [Link](http://deepreader.io/LeetCode/problem_list.html).
716

817
### Feedback
9-
Welcome to raise an issue if you have any feedback.
18+
Welcome to raise an issue [here](https://github.com/zhangdanyangg/LeetCode/issues) if you have any feedback.
1019

1120
### Notes
1221
Failed attempts are kept in the source code, which are annotated as TLE (Time Limit Exceeds) or MLE (Memory Limit Exceeds).
1322

1423
### TODO
15-
Question burn-down rate: 150/152 completed
24+
Question burn-down rate: 151/152 completed
25+
26+
###Miscellaneous
27+
Practice
28+
I am just writing the code for the sake of doing it, since there's lots of fun in learning and writing.
1629

1730
[ascii](https://gist.github.com/xero/3555086)
1831
```
19-
.?77777$$.
20-
.?77777777777777$.
21-
777..777777777777$+
22-
.77 7777777777$$$
23-
.777 .7777777777$$$$
24-
.7777777777777$$$$$$
25-
..........:77$$$$$$$
26-
.77777777777777777$$$$$$$$$.=======.
27-
777777777777777777$$$$$$$$$$.========
28-
7777777777777777$$$$$$$$$$$$$.=========
29-
77777777777777$$$$$$$$$$$$$$$.=========
30-
777777777777$$$$$$$$$$$$$$$$ :========+.
31-
77777777777$$$$$$$$$$$$$$+..=========++~
32-
777777777$$..~=====================+++++
33-
77777777$~.~~~~=~=================+++++.
34-
777777$$$.~~~===================+++++++.
35-
77777$$$$.~~==================++++++++:
36-
7$$$$$$$.==================++++++++++.
37-
.,$$$$$$.================++++++++++~.
38-
.=========~.........
39-
.=============++++++
40-
.===========+++..+++
41-
.==========+++. .++
42-
,=======++++++,,++,
43-
..=====+++++++++=.
44-
..~+=...
32+
.?77777$$.
33+
.?77777777777777$.
34+
777..777777777777$+
35+
.77 7777777777$$$
36+
.777 .7777777777$$$$
37+
.7777777777777$$$$$$
38+
..........:77$$$$$$$
39+
.77777777777777777$$$$$$$$$.=======.
40+
777777777777777777$$$$$$$$$$.========
41+
7777777777777777$$$$$$$$$$$$$.=========
42+
77777777777777$$$$$$$$$$$$$$$.=========
43+
777777777777$$$$$$$$$$$$$$$$ :========+.
44+
77777777777$$$$$$$$$$$$$$+..=========++~
45+
777777777$$..~=====================+++++
46+
77777777$~.~~~~=~=================+++++.
47+
777777$$$.~~~===================+++++++.
48+
77777$$$$.~~==================++++++++:
49+
7$$$$$$$.==================++++++++++.
50+
.,$$$$$$.================++++++++++~.
51+
.=========~.........
52+
.=============++++++
53+
.===========+++..+++
54+
.==========+++. .++
55+
,=======++++++,,++,
56+
..=====+++++++++=.
57+
..~+=...
4558
```

0 commit comments

Comments
 (0)