-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
91 lines (76 loc) · 2.59 KB
/
main.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
vector<string> line;
int len = 0;
auto print = [&]() -> void {
if (line.size() == 1) {
string s =
line.front() + string(maxWidth - line.front().size(), ' ');
result.push_back(s);
} else {
int sp = maxWidth - len;
int base = sp / (line.size() - 1);
int extra = sp % (line.size() - 1);
result.push_back(line.front());
for (int i = 1; i < line.size(); ++i) {
int v = base;
if (--extra >= 0) ++v;
result.back() += string(v, ' ') + line[i];
}
}
line.clear();
len = 0;
};
for (const string& w : words) {
if (len + w.size() + line.size() > maxWidth) print();
line.push_back(w);
len += w.size();
}
result.push_back(line.front());
for (int i = 1; i < line.size(); ++i)
result.back() = result.back() + " " + line[i];
result.back() += string(maxWidth - result.back().size(), ' ');
return result;
}
};
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
auto generate = [&](int l, int r) -> string {
int spaces = maxWidth;
for (int i = l; i < r; ++i) spaces -= words[i].size();
int cnt = r - l - 1;
if (cnt == 0) return words[l] + string(spaces, ' ');
int pad = spaces / cnt;
int rem = spaces % cnt;
string result;
for (int i = l; i < r - 1; ++i) {
result += words[i] + string(pad + (rem-- > 0), ' ');
}
result += words[r - 1];
return result;
};
vector<string> result;
int n = words.size();
for (int l = 0; l < n;) {
int r = l + 1, cnt = words[l].size();
while (r < n) {
cnt += 1 + words[r].size();
if (cnt > maxWidth) break;
++r;
}
if (r < n) {
result.push_back(generate(l, r));
} else {
string s = words[l];
for (int i = l + 1; i < r; ++i) s += " " + words[i];
s += string(maxWidth - s.size(), ' ');
result.push_back(s);
}
l = r;
}
return result;
}
};