leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit f69eb636ed287d5f4711d64791591a283f8a67f8
parent 1b6eb9811b03eb3366e679b374a8c87a4880a2cd
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat, 25 Mar 2023 12:00:15 +0100

Random Problem

Diffstat:
AProblems/0068.cpp | 36++++++++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/Problems/0068.cpp b/Problems/0068.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector<string> fullJustify(vector<string> &words, int maxWidth) { + vector<string> res; + int i = 0, j; + while (i < words.size()) { + int count = words[i].size(); // character count + for (j = i + 1; j < words.size() && count <= maxWidth; j++) + count += words[j].size() + 1; // word lenght + at least 1 space + + if (count > maxWidth) + count -= words[--j].size() + 1; // if we overshot, remove last word + int wordc = j - i; // number of words for the current row + count -= + wordc - 1; // remove minimum padding added from the character count; + int white = maxWidth - count; // how much whitespace + string row = words[i++]; + if (i != j) { + if (j != words.size()) { + while (i < j) { + int segment = ceil((double)white / (wordc-- - 1)); + row += string(min(segment, white), ' ') + words[i++]; + white -= segment; + } + } else { + // last row, adjust left + while (i < j) row += " " + words[i++]; + } + } + row += + string(maxWidth - row.size(), ' '); // padd the remainder of the row + res.push_back(row); // push the current row to the result + } + return res; + } +}; diff --git a/README.md b/README.md @@ -83,6 +83,7 @@ for solving problems. | 0064 | Medium | [Minimum Path Sum](Problems/0064.cpp) | | 0066 | Easy | [Plus One](Problems/0066.cpp) | | 0067 | Easy | [Add Binary](Problems/0067.cpp) | +| 0068 | Hard | [Text Justification](Problems/0068.cpp) | | 0069 | Easy | [Sqrt(x)](Problems/0069.cpp) | | 0070 | Easy | [Climbing Stairs](Problems/0070.cpp) | | 0072 | Hard | [Edit Distance](Problems/0072.cpp) |