-
[LeetCode] 68. Text JustificationLife/PS 2026. 6. 4. 18:41

The code is much dirtier than I thought, but I can't think of a way to make it okay.
class Solution { public: vector<string> fullJustify(vector<string>& words, int maxWidth) { int ch_cnt = 0; vector<string> bag; vector<string> res; for (string& word: words) { if (ch_cnt + !bag.empty() + word.length() > maxWidth) { string build; int num_ws = bag.size() - 1; int tot_ws = maxWidth - (ch_cnt - num_ws); for (int i=0; i<num_ws; i++) { build += bag[i]; build += string((tot_ws/num_ws) + (tot_ws%num_ws>i), ' '); } build += bag[num_ws]; if (build.length() < maxWidth) { build += string(maxWidth - build.length(), ' '); } res.push_back(build); bag.clear(); ch_cnt = 0; } ch_cnt += word.length() + !bag.empty(); bag.push_back(word); } if (!bag.empty()) { string build; int num_ws = bag.size() - 1; for(int i=0; i<num_ws; i++) { build += bag[i]; build += " "; } build += bag[num_ws]; if (build.length() < maxWidth) { build += string(maxWidth - build.length(), ' '); } res.push_back(build); } return res; } };'Life > PS' 카테고리의 다른 글
[LeetCode] 84. Largest Rectangle in Histogram (0) 2026.06.08 [LeetCode] 76. Minimum Window Substring (0) 2026.06.07 [LeetCode] 65. Valid Number (0) 2026.05.31 [LeetCode] 60. Permutation Sequence (0) 2026.05.30 [LeetCode] 52. N-Queens II (0) 2026.05.29