My C++ solution


  • 0
    N
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int width = 0;
        int left = 0;
        vector<string> result;
        for (int i = 0; i < words.size(); ++i) {
            if (width > 0 && width + words[i].size() + 1 > maxWidth) {
                int gaps = i - left - 1;
                int residue = maxWidth - width;
                int extra = gaps ? (residue + gaps - 1) / gaps : 0;
                int boundary = gaps ? gaps - (extra * gaps - residue) : -1;
                string line;
                for (int j = 0; j < i - left; ++j) {
                    if (j - 1 == boundary) --extra;
                    line += (j ? string(extra + 1, ' ') : "") + words[left + j];
                }
                line += string(maxWidth - line.size(), ' ');
                result.push_back(line);
                width = 0;
                left = i;
            }
            width += words[i].size() + (width ? 1 : 0);
        }
        string line;
        for (int i = 0; i < words.size() - left; ++i)
            line += (i ? " " : "") + words[left + i];
        line += string(maxWidth - line.size(), ' ');
        result.push_back(line);
        return result;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.