-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleet_68.cpp
71 lines (64 loc) · 2.09 KB
/
leet_68.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
#include <vector>
#include <string>
#include <iostream>
using namespace std;
// 68: Text Justification
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
// return words;
vector<string> formatted;
// string currLine;
int beginWord = 0;
int lineLength = -1;
for(int i=0; i<words.size(); ++i){
if(lineLength + words[i].length() + 1 > maxWidth){
cout << beginWord << " " << i-1 << '\n';
// submit current line
string currLine;
currLine += words[beginWord];
if(i - beginWord - 1 > 0){
int spacePerLine = (maxWidth - lineLength)/(i - beginWord - 1);
int extraSpaces = (maxWidth - lineLength) % (i - beginWord - 1);
for(int j=beginWord+1; j<i; ++j){
for(int k=0; k<spacePerLine+1; ++k){
currLine += " ";
}
if(extraSpaces){
currLine += " ";
--extraSpaces;
}
currLine += words[j];
}
} else {
while(currLine.size() < maxWidth) currLine += " ";
}
formatted.push_back(currLine);
// preparing for new line
lineLength = -1;
beginWord = i;
}
lineLength += words[i].length() + 1;
}
string currLine;
currLine += words[beginWord];
for(int j=beginWord+1; j<words.size(); ++j){
currLine += " " + words[j];
}
while(currLine.size() < maxWidth) currLine += " ";
formatted.push_back(currLine);
return formatted;
}
};
int main(){
// vector<string> words = {"This", "is", "an", "example", "of", "text", "justification."};
// Solution sol;
// vector<string> formatted = sol.fullJustify(words, 16);
// vector<string> words = {"This", "is", "an", "example", "of", "text", "justification."};
// Solution sol;
// vector<string> formatted = sol.fullJustify(words, 14);
vector<string> words = {"justification."};
Solution sol;
vector<string> formatted = sol.fullJustify(words, 14);
for(auto &x: formatted) cout << x << '\n';
}