-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-justification.cpp
38 lines (38 loc) · 1.23 KB
/
text-justification.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
37
38
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res;
int n = words.size();
int k = 0;
while (k < n) {
int strlength = 0;
int i = k;
while (i < n && words[i].length() + strlength <= maxWidth) {
strlength += words[i].length() + 1;
i++;
}
int spaceCount = maxWidth - strlength + i - k;
string tmp;
if (i >= n)
for (int j = k; j < i; j++) {
tmp += words[j];
int spaces = tmp.size() < maxWidth ? 1 : 0;
tmp += string(spaces, ' ');
spaceCount -= spaces;
}
else
for (int j = k; j < i; j++) {
tmp += words[j];
int spaces =
(i - j - 1) ? ceil((float)spaceCount / (i - j - 1)) : 0;
spaceCount -= spaces;
tmp += string(spaces, ' ');
}
if (spaceCount > 0)
tmp += string(spaceCount, ' ');
res.push_back(tmp);
k = i;
}
return res;
}
};