-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_68.cpp
More file actions
46 lines (38 loc) · 1.65 KB
/
Copy pathleetcode_68.cpp
File metadata and controls
46 lines (38 loc) · 1.65 KB
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
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
int n = words.size();
int index = 0;
while (index < n) {
int totalChars = words[index].size();
int last = index + 1;
// 현재 줄에 넣을 수 있는 단어 결정
while (last < n && totalChars + words[last].size() + (last - index) <= maxWidth) {
totalChars += words[last].size();
last++;
}
int gaps = last - index - 1;
string line = words[index];
// 마지막 줄이거나 한 단어만 있는 경우: 왼쪽 정렬
if (last == n || gaps == 0) {
for (int i = index + 1; i < last; i++) {
line += " " + words[i];
}
line += string(maxWidth - line.size(), ' '); // 남은 공간 공백 채우기
}
// 그 외의 경우: 균등 분배
else {
int spaces = (maxWidth - totalChars) / gaps;
int extraSpaces = (maxWidth - totalChars) % gaps;
for (int i = index + 1; i < last; i++) {
int spaceCount = spaces + (i - index <= extraSpaces ? 1 : 0);
line += string(spaceCount, ' ') + words[i];
}
}
result.push_back(line);
index = last;
}
return result;
}
};