Sunday, December 22, 2013

Day 62, #68, #69, Text Justification, Sqrt(x)

Text Justification
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 L 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.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.
Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Note: Each word is guaranteed not to exceed L in length.
Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
--------------------------------
This is one of the most muthaFking types of questions. It makes people suicidal until it  passes the OJ.
class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        int used = 0;
        vector<string> ret;
        vector<string> temp;
        if (L == 0) {
            ret.push_back("");
            return ret;
        }
        
        bool flag =false;
        for (int i = 0; i < words.size(); i++) {
            // always have one element in the temp. ease the pain
            if (!flag) {
                temp.clear();
                used = 0;
                used = words[i].length();
                temp.push_back(words[i]);
                flag = true;
            }else {
                // keep feeding the temp
                if (words[i].length() + 1 + used <= L) {
                    temp.push_back(" " + words[i]);
                    used += 1 + words[i].length();
                }
                
                // if used + current word's length is larger than L
                // clear temp, form the line and push it to ret
                else {
                    i--; // back by one step
                    flag = false;
                    if (temp.size() == 1) {
                        string str = temp[0];
                        for (int j = 0; j < L - used; j++) {
                            str += " ";
                        }
                        ret.push_back(str);
                        continue;
                    }
                    
                    // calculate extra space between words 
                    int spaceCount = (L - used) / (temp.size() - 1);
                    int extraSpace = (L - used) % (temp.size() - 1);
                    string space = "";
                    for (int j = 0; j < spaceCount; j++) {
                        space += " ";
                    }
                    
                    string str = temp[0];
                    for (int j = 1; j < temp.size(); j++) {
                        if (extraSpace > 0) {
                            str += " ";
                            extraSpace--;
                        }
                        str += space + temp[j];
                    }
                    ret.push_back(str);
                }
            }
        }
        
        // dealing with the last line
        if (flag) {
            string str = temp[0];
            for (int i = 1; i < temp.size(); i++) {
                str += temp[i];
            }
            for (int i = used; i < L; i++) {
                str += " ";
            }
            ret.push_back(str);
        }
        
        return ret;
    }
};
Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
--------------------------------------------------
Solution#1, binary search
why always return right in the end??
A: Inorder to break out the loop, we either have sq == x or left > right, for this Sqrt(x) method, we need to return the lower bound(ex. input is 250, return 15, not 16)
class Solution {
public:
    int sqrt(int x) {
        long long left = 0;
        long long right = x /2 + 1;
        while (left <= right) {
            long long mid = (left + right) / 2;
            long long sq = mid * mid;
            if (sq == x) {
                return mid;
            }
            if (sq < x) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return right; // why always return right
    }
};
Solution#2
Newton's method
Update on Jan-23-2015 
could be if (fabs(i - j) < 0.000001) break;
class Solution {
public:
    int sqrt(int x) {
        if (x == 0) return 0;
        double i = 1.0;
        double j = 1;
        while (true) {
            j = (i + x / i) / 2.0;
            if (i == j) break;
            i = j;
        }
        
        return (int)j;
    }
};

No comments:

Post a Comment