Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 68 · Other

Text Justification

LeetCode 68 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Greedy packing + even/remainder space distribution.

கடினம்ArrayStringSimulation
LeetCode இல் திற ↗

நான் Text Justification (LeetCode 68) ஐ இப்படித் தீர்த்தேன் — Other பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Greedily pack as many words as fit on each line. Then distribute the leftover spaces between words, giving the leftmost gaps one extra space when they don't divide evenly. The last line, and any one-word line, is left-justified instead.

Approach

  1. 1

    create_line: take words while length + word ≤ maxWidth (counting one space each).

  2. 2

    extra = maxWidth − (letters + minimum spaces).

  3. 3

    Last line or single word: join with single spaces and pad the right.

  4. 4

    Otherwise give each gap extra // gaps spaces and the first extra % gaps gaps one more.

நான் எப்படித் தீர்த்தேன்

Greedy line builderPythonO(n) time
class Solution:
    def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
        #get no of words in each line - create_line
        #Prepare line with spaces 
        #append the line

        def create_line(i):
            line = []
            cur_length = 0
            while i<len(words) and cur_length+len(words[i])<=maxWidth:
                cur_length+=len(words[i])+1
                line.append(words[i])
                i+=1
            return line
        def prepare_line(i,line):
            #prepare what are the word counts, each word will have one space except last word
            #extra space maxwidhth- (each word+1)=>baseline-1
            #if we are at end line, or there is only one word, just append spaces 
            #if not then add odd spaces on left, even spaces based on word count
            #return the final line
            word_counts=len(line)-1
            baseline=0 
            for word in line:
                baseline+=len(word)+1
            baseline-=1 #don't need for last word
            extra_space = maxWidth-baseline
            if i==len(words) or len(line)==1:
                return " ".join(line)+" "*extra_space
            even_spaces = extra_space//word_counts
            odd_spaces = extra_space%word_counts
            for i in range(odd_spaces):
                line[i]+=" "
            for i in range(word_counts):
                line[i]+=" "*even_spaces
            return " ".join(line)
        res=[]
        i=0
        while i<len(words):
            line = create_line(i)
            i+=len(line)
            res.append(prepare_line(i,line)) #i is sent post increment to check whether we have reached at the end of the line
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Text Justification — அடிக்கடி கேட்கப்படும் கேள்விகள்

Text Justification பின்னாலுள்ள intuition என்ன?

Greedily pack as many words as fit on each line. Then distribute the leftover spaces between words, giving the leftmost gaps one extra space when they don't divide evenly. The last line, and any one-word line, is left-justified instead.

Text Justification (LeetCode 68) ஐ எப்படித் தீர்ப்பது?

1. create_line: take words while length + word ≤ maxWidth (counting one space each). 2. extra = maxWidth − (letters + minimum spaces). 3. Last line or single word: join with single spaces and pad the right. 4. Otherwise give each gap extra // gaps spaces and the first extra % gaps gaps one more.

Text Justification இன் time மற்றும் space complexity என்ன?

Time: O(total characters). Space: O(maxWidth). (Accepted approach: Greedy line builder.)

Text Justification இல் பொதுவான தவறுகள் என்ன?

Left gaps get the remainder, not the right ones.