-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
133 lines (116 loc) · 4.08 KB
/
Copy pathsolution.cpp
File metadata and controls
133 lines (116 loc) · 4.08 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
/**
* Problem: 68. Text Justification
* Difficulty: Hard
* Topics: Array, String, Simulation, Greedy
* LeetCode Link: https://leetcode.com/problems/text-justification/
*
* Time Complexity: O(Total Characters) - linear pass over words and characters
* Space Complexity: O(maxWidth) auxiliary buffer per line
*/
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> result;
int n = static_cast<int>(words.size());
int i = 0;
while (i < n) {
// 1. Determine how many words fit in the current line
int lineLen = static_cast<int>(words[i].length());
int j = i + 1;
while (j < n && lineLen + 1 + static_cast<int>(words[j].length()) <= maxWidth) {
lineLen += 1 + static_cast<int>(words[j].length());
++j;
}
int numWords = j - i;
int numGaps = numWords - 1;
// Calculate total word character count (excluding spaces)
int wordChars = 0;
for (int k = i; k < j; ++k) {
wordChars += static_cast<int>(words[k].length());
}
int totalSpaces = maxWidth - wordChars;
string lineStr = "";
// 2. Format line based on justification rules
// Case A: Last line OR single word in line -> Left-justified
if (j == n || numGaps == 0) {
for (int k = i; k < j; ++k) {
if (k > i) lineStr += " ";
lineStr += words[k];
}
// Pad trailing spaces
lineStr += string(maxWidth - lineStr.length(), ' ');
} else {
// Case B: Fully-justified line
int baseSpaces = totalSpaces / numGaps;
int extraSpaces = totalSpaces % numGaps;
for (int k = i; k < j - 1; ++k) {
int currentGap = baseSpaces + ((k - i) < extraSpaces ? 1 : 0);
lineStr += words[k];
lineStr += string(currentGap, ' ');
}
// Append the last word in the line
lineStr += words[j - 1];
}
result.push_back(lineStr);
i = j;
}
return result;
}
};
// ==========================================
// Local Test Runner (Guarded for LeetCode Submission)
// ==========================================
#ifdef LOCAL_TEST
int main() {
Solution solver;
// Test Case 1
{
vector<string> words = {"This", "is", "an", "example", "of", "text", "justification."};
int maxWidth = 16;
vector<string> expected = {
"This is an",
"example of text",
"justification. "
};
assert(solver.fullJustify(words, maxWidth) == expected);
cout << "Test 1 Passed!" << endl;
}
// Test Case 2
{
vector<string> words = {"What", "must", "be", "acknowledgment", "shall", "be"};
int maxWidth = 16;
vector<string> expected = {
"What must be",
"acknowledgment ",
"shall be "
};
assert(solver.fullJustify(words, maxWidth) == expected);
cout << "Test 2 Passed!" << endl;
}
// Test Case 3
{
vector<string> words = {
"Science", "is", "what", "we", "understand", "well", "enough", "to",
"explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"
};
int maxWidth = 20;
vector<string> expected = {
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
};
assert(solver.fullJustify(words, maxWidth) == expected);
cout << "Test 3 Passed!" << endl;
}
cout << "All test cases passed successfully!" << endl;
return 0;
}
#endif