-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0068-Text_Justification.cpp
More file actions
163 lines (151 loc) · 4.66 KB
/
0068-Text_Justification.cpp
File metadata and controls
163 lines (151 loc) · 4.66 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************
* 0068-Text_Justification.cpp
* Billy.Ljm
* 24 August 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/text-justification/
*
* Given an array of strings words and a width maxWidth, format the text such
* that each line has exactly maxWidth characters and is fully (left and right)
* justified.
*
* You should pack your words in a greedy approach; that is, pack as many words
* as you can in each line. Pad extra spaces ' ' when necessary so that each
* line has exactly maxWidth characters.
*
* Extra spaces between words should be distributed as evenly as possible. If
* the number of spaces on a line does not divide evenly between words, the
* empty slots on the left will be assigned more spaces than the slots on the
* right.
*
* For the last line of text, it should be left-justified, and no extra space is
* inserted between words.
*
* ===========
* My Approach
* ===========
* We will first choose as many words as can fit into the current line, with
* spaces. Then, we calculate the extra white-spaces left and distribute them
* accordingly between each word. This repeats until all the words have be
* justified.
*
* This has a time complexity of O(n), and a space complexity of O(n), where n
* is the number of words.
******************************************************************************/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> currline, out;
int currwidth = 0 , nspaces;
// justify all words
for (string word : words) {
// if word overflows, return
if (word.size() > maxWidth) return vector<string>();
// append word to current line
else if (currwidth + word.size() <= maxWidth) {
currline.push_back(word);
currwidth = currwidth + word.size() + 1;
}
// output single word line
else if (currline.size() == 1) {
out.push_back(currline[0]);
out.back() = out.back() + string(maxWidth - out.back().size(), ' ');
// append first word of next line
currline = { word };
currwidth = word.size() + 1;
}
// output current line and create new line
else {
// calculate whitespaces
currwidth--; // remove trailing whitespace
nspaces = (maxWidth - currwidth) / (currline.size() - 1);
currwidth = currwidth + nspaces * (currline.size() - 1);
// create line
out.push_back("");
for (int i = 0; i < maxWidth - currwidth; i++) {
out.back() = out.back() + currline[i];
out.back() = out.back() + string(nspaces + 2, ' ');
}
for (int i = maxWidth - currwidth; i < currline.size(); i++) {
out.back() = out.back() + currline[i];
if (i != currline.size() - 1) {
out.back() = out.back() + string(nspaces + 1, ' ');
}
}
// append first word of next line
currline = { word };
currwidth = word.size() + 1;
}
}
// last line
out.push_back("");
for (int i = 0; i < currline.size(); i++) {
out.back() = out.back() + currline[i];
if (i != currline.size() - 1) {
out.back() = out.back() + ' ';
}
}
out.back() = out.back() + string(maxWidth - out.back().size(), ' ');
return out;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<string> words;
int maxWidth;
// test case 1
words = { "This","is","an","example","of","text","justification." };
maxWidth = 16;
std::cout << "fullJustify(" << words << ", " << maxWidth << ") = " << endl;
std::cout << "[" << endl;
for (string s : sol.fullJustify(words, maxWidth)) {
std::cout << " " << s << endl;
}
std::cout << "]" << endl;
// test case 2
words = { "What","must","be","acknowledgment","shall","be" };
maxWidth = 16;
std::cout << "fullJustify(" << words << ", " << maxWidth << ") = " << endl;
std::cout << "[" << endl;
for (string s : sol.fullJustify(words, maxWidth)) {
std::cout << " " << s << endl;
}
std::cout << "]" << endl;
// test case 3
words = { "Science","is","what","we","understand","well","enough","to",
"explain","to","a","computer.","Art","is","everything","else","we","do" };
maxWidth = 20;
std::cout << "fullJustify(" << words << ", " << maxWidth << ") = " << endl;
std::cout << "[" << endl;
for (string s : sol.fullJustify(words, maxWidth)) {
std::cout << " " << s << endl;
}
std::cout << "]" << endl;
return 0;
}