Skip to content

Commit 8a045a0

Browse files
author
Partho Biswas
committed
68. Text Justification
1 parent 1ee1613 commit 8a045a0

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
529529
|14| [253. Meeting Rooms II](https://tinyurl.com/r8vr7qo)| [Python](https://tinyurl.com/wu6rdaw/253_Meeting_Rooms_II.py)| [Official](https://leetcode.com/problems/meeting-rooms-ii/solution/) | Medium | Greedy and uses heap |
530530
|15| [759. Employee Free Time](https://tinyurl.com/y5z73vre)| [Python](https://tinyurl.com/wu6rdaw/759_Employee_Free_Time.py)| [Educative.io](https://tinyurl.com/v42vmyr) | Hard | Greedy and uses heap. Not done. Check again |
531531
|16| [659. Split Array into Consecutive Subsequences](https://tinyurl.com/vsbddyl) | [Python](https://tinyurl.com/wu6rdaw/659_Split_Array_into_Consecutive_Subsequences.py)| [Art 0](https://tinyurl.com/qq5jucw), [Art 1](https://tinyurl.com/qk7m78s), [Art 2](https://tinyurl.com/sjkdtre), [Art 3](https://tinyurl.com/tnewgqa), **[Vid 1](https://tinyurl.com/u2hnq84)**, **[Art 4](https://tinyurl.com/w3j4yqp)** | Medium | 📌 It's a fucking unclear problem. TODO: Not done. Check again later |
532+
|16| **[843. Guess the Word](https://tinyurl.com/vyk9ynp)** | [Python](https://tinyurl.com/wu6rdaw/843_Guess_the_Word.py)| --- | Hard | TODO: NOT DONE |
533+
|16| **[68. Text Justification](https://tinyurl.com/r7bzsul)** | [Python](https://tinyurl.com/wu6rdaw/68_Text_Justification.py)| [Vid 1](https://tinyurl.com/jkx2usg), [Vid 2](https://tinyurl.com/wd7rja2), [Vid 3](https://tinyurl.com/qlbsfqf), [Art 0](https://tinyurl.com/u5r5cuy), [Art 1](https://tinyurl.com/s84dym2) | Hard | TODO: Check again later. Very Important |
532534

533535
</p>
534536
</details>

leetcode.com/python/68_Text_Justification.py

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution(object):
2+
def fullJustify(self, words, maxWidth):
3+
"""
4+
:type words: List[str]
5+
:type maxWidth: int
6+
:rtype: List[str]
7+
"""
8+
currentWordIdx = 0 # the index of the current word
9+
ans = []
10+
while currentWordIdx < len(words):
11+
wordCountForEachLine = self.getWordsCountForEachLine(currentWordIdx, words, maxWidth)
12+
line = self.insertSpaces(currentWordIdx, wordCountForEachLine, words, maxWidth) # create a line which contains words from words[currentWordIdx] to words[currentWordIdx + wordCountForEachLine - 1]
13+
ans.append(line)
14+
currentWordIdx += wordCountForEachLine
15+
return ans
16+
17+
18+
# How many words we need to form each line;
19+
def getWordsCountForEachLine(self, currentWordIdx, words, maxWidth):
20+
wordCountForLine = 1 # figure out how many words can fit into a line, which starts from 1 ssince we are adding one word initially
21+
currentLen = len(words[currentWordIdx])
22+
while currentWordIdx + wordCountForLine < len(words):
23+
nextLen = len(words[currentWordIdx + wordCountForLine]) + 1 # here, the last added 1 is for an added space at the left
24+
if currentLen + nextLen <= maxWidth:
25+
wordCountForLine += 1
26+
currentLen += nextLen
27+
else:
28+
break
29+
return wordCountForLine
30+
31+
32+
33+
# How many spaces we should insert between two words.
34+
def insertSpaces(self, currentWordIdx, wordCountForEachLine, words, maxWidth):
35+
initialLine = ' '.join(words[currentWordIdx:currentWordIdx + wordCountForEachLine]) # Concatenate words[currentWordIdx:currentWordIdx + wordCountForEachLine] into one line
36+
finalLine = ""
37+
if wordCountForEachLine == 1 or currentWordIdx + wordCountForEachLine == len(words): # if the line contains only one word or it is the last line
38+
spaces = maxWidth - len(initialLine) # we just need to left assigned it
39+
finalLine = initialLine + ' '*spaces
40+
else:
41+
totalSpaces = maxWidth - len(initialLine) + (wordCountForEachLine - 1) # total number of spaces we need insert
42+
averageSpaces = totalSpaces // (wordCountForEachLine - 1) # average number of spaces we should insert between two words
43+
leftWords = totalSpaces % (wordCountForEachLine - 1) # number of 'left' words, i.e. words that have 1 more space than the other words on the right side
44+
if leftWords > 0:
45+
finalLine = (" "*(averageSpaces + 1)).join(words[currentWordIdx:currentWordIdx + leftWords]) # left words. The reason for +1 is, these words will have one more spaces then others
46+
finalLine += " "*(averageSpaces + 1) # spaces between left words & right words
47+
finalLine += (" "*averageSpaces).join(words[currentWordIdx + leftWords:currentWordIdx + wordCountForEachLine]) # right words
48+
else:
49+
finalLine = (" "*averageSpaces).join(words[currentWordIdx:currentWordIdx + wordCountForEachLine])
50+
return finalLine
51+

0 commit comments

Comments
 (0)