← Home

68. Text Justification

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
68

The trick here is to name the state correctly, then let the implementation follow. For 68. Text Justification, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are fullJustify.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Text Justification.
02//Memory Usage: 7.5 MB, less than 53.09% of C++ online submissions for Text Justification.
03class Solution {
04public:
05    vector<string> fullJustify(vector<string>& words, int maxWidth) {
06        vector<string> line;
07        int lineSize = 0;
08        int cur = 0;
09        int n = words.size();
10        vector<string> ans;
11        
12        while(cur < n){
13            // cout << "cur: " << cur << endl;
14            if(lineSize + (line.size()-1) + (1+words[cur].size()) <= maxWidth){
15                // cout << "build line" << endl;
16                lineSize += words[cur].size();
17                line.push_back(words[cur]);
18                ++cur;
19            }else{
20                // cout << "flush" << endl;
21                //cannot add words into this line anymore
22                string str = "";
23                if(line.size() == 1){
24                    str += line[0];
25                    str += string(maxWidth - str.size(), ' ');
26                }else{
27                    int quot = (maxWidth - lineSize)/(line.size()-1);
28                    int mod = (maxWidth - lineSize)%(line.size()-1);
29                    
30                    for(int i = 0; i < line.size(); ++i){
31                        str += line[i];
32                        if(i != line.size()-1)
33                            str += string(quot + (i<mod), ' ');
34                    }
35                }
36                ans.push_back(str);
37                line.clear();
38                lineSize = 0;
39            }
40            // ++cur;
41        }
42        
43        //flush the last line
44        string str = "";
45        //last line must be left-justified
46        for(int i = 0; i < line.size(); ++i){
47            str += line[i];
48            if(i != line.size()-1) str += " ";
49        }
50        str += string(maxWidth - str.size(), ' ');
51        ans.push_back(str);
52        line.clear();
53        lineSize = 0;
54        
55        ans.back() += string(maxWidth-ans.back().size(), ' ');
56        
57        return ans;
58    }
59};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.