LeetCode 68 Text Justification (C++, Python)

Given an array of words and a length L, format the text such that each line has exactly L 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 Lcharacters.
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.
For example,
words["This", "is", "an", "example", "of", "text", "justification."]
L16.
Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.

Solution C++


 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
private:
    void justifyWords(vector<string>& words, int start, int end, int diff, vector<string>& ret) {
        int space = (end == start + 1)? diff: (diff / (end - 1 - start));
        int extra = (end == start + 1)? 0: (diff % (end - 1 - start));
        string row;
        if (end == start + 1) {
            row = words[start];
            row.append(space, ' ');
        } else {
            for (int j = start; j < end; ++j) {
                row += words[j];
                if (j == end - 1) continue;
                row.append(space, ' ');
                if (extra > 0) {
                    row.push_back(' ');
                    extra --;
                }
            }
        }
        ret.push_back(row);
    }
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ret;
        int start = 0;
        int i = 0;
        int totalsize = 0;
        while (i < words.size()) {
            int curlen = words[i].length();
            totalsize += curlen;
            if (totalsize + (i - start) > maxWidth) {
                totalsize -= curlen;
                justifyWords(words, start, i, maxWidth - totalsize, ret);
                start = i;
                totalsize = 0;
            } else {
                ++i;
            }
        }
        string row;
        for (int j = start; j < i; ++j) {
            row += words[j];
            if (j == i - 1) {
                int r = maxWidth - row.length();
                if (r > 0) {
                    row.append(r, ' ');
                }
            } else {
                row.push_back(' ');
            }
        }
        ret.push_back(row);
        return ret;
    }
};

Solution Python


 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
37
38
39
40
41
42
43
class Solution:
    # @param {string[]} words
    # @param {integer} maxWidth
    # @return {string[]}
    def fullJustify(self, words, maxWidth):
        ret = []
        start = end = 0
        totallen = 0;
        while end < len(words):
            if totallen + len(words[end]) + end - start > maxWidth:
                space = (maxWidth - totallen) if (end == start + 1) else (maxWidth - totallen) / (end - 1 - start)
                extra = 0 if (end == start + 1) else (maxWidth - totallen) % (end - 1 - start)
                tmp = []
                if end == start + 1:
                    tmp.append(words[start])
                    tmp.append(' ' * space)
                else:
                    for i in range(start, end):
                        tmp.append(words[i])
                        if i == end - 1:
                            continue
                        tmp.append(' ' * space)
                        if extra:
                            tmp.append(' ')
                            extra -= 1
                ret.append(''.join(tmp))
                start = end
                totallen = 0
            else:
                totallen += len(words[end])
                end += 1
        tmp = []
        padding = maxWidth - totallen
        for i in range(start, end):
            tmp.append(words[i])
            if i == end - 1:
                tmp.append(' ' * padding)
            else:
                tmp.append(' ')
                padding -= 1
        ret.append(''.join(tmp))
        return ret
        

Popular posts from this blog

How Django Works (4) URL Resolution

Python Class Method and Class Only Method