-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
36 lines (36 loc) · 1.32 KB
/
soln.cpp
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
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ans;
const int n = words.size();
for(int i = 0, k, l; i < n; i += k) {
// k = 0; // number of words in current line
// l = 0; // summation of length of words in current line
for(k = 0, l = 0; i + k < n && l + words[i + k].size() + k <= maxWidth; ++k) {
l += words[i + k].size();
}
string temp = "";
if (i + k == n) {
for(int j = 0; j < k; ++j) temp += words[i + j] + " ";
temp.pop_back();
temp += string(maxWidth - temp.size(), ' ');
} else {
// pad spaces
int nspaces = maxWidth - l;
if (k == 1) {
temp += words[i] + string(nspaces, ' ');
} else {
int r = nspaces % (k - 1);
int q = nspaces / (k - 1);
for(int j = 0; j < k - 1; ++j) {
temp += words[i + j] + string(q, ' ');
if (j < r) temp += ' ';
}
temp += words[i + k - 1];
}
}
ans.push_back(temp);
}
return ans;
}
};