https://leetcode.com/problems/text-justification/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int n = words.size();
        int idx = 0;
        vector<string> res;
        while (idx < n) {
            vector<string> tmp;
            int len = 0;
            while (idx < n && (len + words[idx].size() + (tmp.size() > 0)) <= maxWidth) {
                tmp.push_back(words[idx]);
                len += words[idx].size();
                if (tmp.size() > 1) len += 1;
                idx += 1;
            }
            int sz = tmp.size();
            if (idx == n) {
                string str = "";
                for (int i = 0; i < sz; i++) {
                    str += tmp[i];
                    if (i != (sz - 1)) str.push_back(' ');
                }
                str += string(maxWidth - str.size(), ' ');
                res.push_back(str);
                continue;
            }
            if (sz == 1) {
                string str = tmp[0];
                str += string(maxWidth - str.size(), ' ');
                res.push_back(str);
                continue;
            }
            int left = (maxWidth - len) / (sz - 1);
            int more = (maxWidth - len) % (sz - 1);
            string str = "";
            for (int i = 0; i < sz; i++) {
                str += tmp[i];
                if (i != (sz - 1)) {
                    str += string(left, ' ');
                    if (more) {
                        str.push_back(' ');
                        more -= 1;
                    }
                    str.push_back(' ');
                }
            }
            str += string(maxWidth - str.size(), ' ');
            res.push_back(str);
        }
        return res;
    }
};

Time Complexity: O(n)
Space: O(n)

就是有點麻煩的實作題目而已,稍微細心點就可以