Saturday, April 4, 2015

LeetCode [68] Text Justification

68. Text Justification
Hard
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                  "
]
 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
57
58
class Solution
{
  public:
    vector<string> fullJustify(vector<string> &words, int maxWidth)
    {
        vector<string> ret;
        int sz = words.size(), start = 0;

        while (start < sz)
        {
            //==prepare the line
            //len(current len of line); netlen(sum of words len); cnt(number of words)
            int len = 0, netlen = 0, cnt = 0, end = start;
            //find the last word (end-1) fitting in the line
            while (end < sz && (len + words[end].size() + (len ? 1 : 0)) <= maxWidth)
            {
                len = len + words[end].size() + (len ? 1 : 0);
                netlen += words[end].size(); cnt++; end++;
            }
            int space = cnt > 1 ? (maxWidth - netlen) / (cnt - 1) : 0;
            int cntLongerSpace = cnt > 1 ? (maxWidth - netlen) % (cnt - 1) : 0;

            //==compose the line
            string line;
            int i = start;
            if (end >= sz) //last line
            {
                while (i < end)
                {
                    line += words[i];
                    if (i < end-1) line += " ";
                    i++;
                }
                while (line.size() < maxWidth) line += " ";
            }
            else
            {
                for (int i = start; i < end; ++i)
                {
                    line += words[i];
                    if (i < end - 1) //not the last word
                    {
                        line.append(space, ' ');
                        if ((i - start < cntLongerSpace)) line.append(1, ' ');
                    }
                    else
                    {
                        while (line.size() < maxWidth) line += " ";
                    }
                }
            }

            ret.push_back(line);
            start = end;
        }
        return ret;
    }
};

 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 {
    public List<String> fullJustify(String[] words, int maxWidth) {
        int n = words.length;
        int i = 0;
        List<String> list = new ArrayList<>();
        while(i<n){
            int start = i;
            int len = 0;
            int netLen = 0;
            int cnt = 0;
            while(i<n && (len + (len==0?0:1) + words[i].length())<=maxWidth){
                len += (len==0?0:1) + words[i].length();
                netLen += words[i].length();
                cnt++;
                i++;
            }
            StringBuilder sb = new StringBuilder();
            if(i==n || cnt==1){//align to the left
                for(int j=start; j<i; ++j){
                    if(sb.length()!=0) sb.append(" ");
                    sb.append(words[j]);
                }
                while(sb.length()<maxWidth) sb.append(" ");
            }else{
                int spaceCnt = maxWidth - netLen;
                int spaceLen = spaceCnt/(cnt-1);
                int reminder = spaceCnt%(cnt-1);
                for(int j=start; j<i; ++j){
                    if(sb.length()!=0){
                        for(int k=0; k<spaceLen; ++k) sb.append(" ");
                        //the index of gaps, not the index of words
                        if(j-start-1<reminder) sb.append(" ");
                    }
                    sb.append(words[j]);
                }
            }

            list.add(sb.toString());
        }

        return list;
    }
}

No comments:

Post a Comment