Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

Note:

A word is defined as a character sequence consisting of non-space characters only. Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. The input array words contains at least one word. Example 1:

Input: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 Output: [ "This is an", "example of text", "justification. " ] Example 2:

Input: words = ["What","must","be","acknowledgment","shall","be"] maxWidth = 16 Output: [ "What must be", "acknowledgment ", "shall be " ] Explanation: Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word. Example 3:

Input: words = ["Science","is","what","we","understand","well","enough","to","explain", "to","a","computer.","Art","is","everything","else","we","do"] maxWidth = 20 Output: [ "Science is what we", "understand well", "enough to explain to", "a computer. Art is", "everything else we", "do " ]

My code:

import numpy as np
class Solution:
    def fullJustify(self, words, maxWidth):
        """
        :type words: List[str]
        :type maxWidth: int
        :rtype: List[str]
        """
        result=[]
        start_index=0
        add_label=False
        count_length=0
        count_number=0
        for wid,item in enumerate(words):
            if add_label==False:
                count_length+=len(item)
                count_number+=1
            if count_length>maxWidth:
                if count_number>2:
                    print('count length %d, count number %d'%(count_length,count_number))
                    space_length=int(np.floor((maxWidth-count_length+len(item)+1)/(count_number-2)))+1
                    more_space=maxWidth-count_length+len(item)+1-(space_length-1)*(count_number-2)
                else:
                    space_length=1+maxWidth-count_length+len(item)
                    more_space=0
                print('space length %d, compensate space length %d'%(space_length,more_space))
                tmp_str=""
                for add_id in range(start_index,start_index+more_space):
                    tmp_str=tmp_str+words[add_id]+" "*(space_length+1)
                for add_id in range(start_index+more_space,wid):
                    if add_id==wid-1:
                        tmp_str=tmp_str+words[add_id] 
                        if wid-(start_index+more_space)==1:
                            tmp_str=tmp_str+' '*(maxWidth-len(words[add_id]))
                        continue
                    tmp_str=tmp_str+words[add_id]+" "*(space_length)
                result.append(tmp_str)
                start_index=wid
                count_number=1
                count_length=len(item)+1
                print('wid %d, wid str %s'%(wid,words[wid]))
                continue
            count_length+=1
        #process the final part to left 
        tmp_str=""
        for cid in range(start_index,len(words)):
            if cid==len(words)-1:
                pre_length=len(tmp_str)+len(words[cid])
                tmp_str=tmp_str+words[cid]+' '*(maxWidth-pre_length)               
                continue                 
                
            tmp_str=tmp_str+words[cid]+' '
        result.append(tmp_str)
        return result

Solution: https://leetcode.com/problems/text-justification/