{"id":5688,"date":"2019-10-01T23:30:32","date_gmt":"2019-10-02T06:30:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5688"},"modified":"2019-10-01T23:30:42","modified_gmt":"2019-10-02T06:30:42","slug":"leetcode-68-text-justification","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-68-text-justification\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 68. Text Justification"},"content":{"rendered":"\n<p>Given an array of words and a width&nbsp;<em>maxWidth<\/em>, format the text such that each line has exactly&nbsp;<em>maxWidth<\/em>&nbsp;characters and is fully (left and right) justified.<\/p>\n\n\n\n<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces&nbsp;<code>' '<\/code>&nbsp;when necessary so that each line has exactly&nbsp;<em>maxWidth<\/em>&nbsp;characters.<\/p>\n\n\n\n<p>Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.<\/p>\n\n\n\n<p>For the last line of text, it should be left justified and no&nbsp;<strong>extra<\/strong>&nbsp;space is inserted between words.<\/p>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A word is defined as a character sequence consisting&nbsp;of non-space characters only.<\/li><li>Each word&#8217;s length is&nbsp;guaranteed to be greater than 0 and not exceed&nbsp;<em>maxWidth<\/em>.<\/li><li>The input array&nbsp;<code>words<\/code>&nbsp;contains at least one word.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\nwords = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\nmaxWidth = 16\n<strong>Output:<\/strong>\n[\n&nbsp; &nbsp;\"This &nbsp; &nbsp;is &nbsp; &nbsp;an\",\n&nbsp; &nbsp;\"example &nbsp;of text\",\n&nbsp; &nbsp;\"justification. &nbsp;\"\n]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\nwords = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\nmaxWidth = 16\n<strong>Output:<\/strong>\n[\n&nbsp; \"What &nbsp; must &nbsp; be\",\n&nbsp; \"acknowledgment &nbsp;\",\n&nbsp; \"shall be &nbsp; &nbsp; &nbsp; &nbsp;\"\n]\n<strong>Explanation:<\/strong> Note that the last line is \"shall be    \" instead of \"shall     be\",\n&nbsp;            because the last line must be left-justified instead of fully-justified.\n             Note that the second line is also left-justified becase it contains only one word.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\nwords = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n&nbsp;        \"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\nmaxWidth = 20\n<strong>Output:<\/strong>\n[\n&nbsp; \"Science &nbsp;is &nbsp;what we\",\n  \"understand &nbsp; &nbsp; &nbsp;well\",\n&nbsp; \"enough to explain to\",\n&nbsp; \"a &nbsp;computer. &nbsp;Art is\",\n&nbsp; \"everything &nbsp;else &nbsp;we\",\n&nbsp; \"do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"\n]<\/pre>\n\n\n\n<p>Solution: Simulation<\/p>\n\n\n\n<p>Time complexity: O(sum(len(s))<br>Space complexity: O(sum(len(s)) <\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> fullJustify(vector<string> &words, int L) {\n    size_t i = 0, n = words.size();\n    vector<string> ans;\n    \n    while (i < n) {\n      bool last_line = false;\n      int ll = 0;\n      vector<string_view> tw;\n      \n      while (ll <= L &#038;&#038; i < n) {\n        size_t wl = words[i].length();\n        int tll = ll + (tw.size() ? 1 : 0) + wl;\n        if (tll > L) break;\n        ll = tll;\n        tw.push_back(string_view(words[i]));\n        if (++i == n) last_line = true;\n        if (ll == L) break;\n      }\n\n      string line;\n      if (!last_line) {\n        int tl = 0; \n        for(const auto& w : tw) tl += w.length();\n        int avg_space = tw.size()==1 ? 0 : ((L-tl) \/ (tw.size() - 1));\n        int extra_space = tw.size()==1 ? 0 : (L - avg_space * (tw.size() - 1) - tl);        \n        for (const auto& w : tw) {\n          if (line.length() > 0) {\n            line.append(avg_space, ' ');\n            if (extra_space > 0) { \n              line += ' '; \n              --extra_space; \n            }\n          }\n          line += w;\n        }\n      } else {\n        for (const auto& w : tw) {\n          if (line.length() > 0) \n            line += ' ';\n          line += w;\n        }\n      }\n\n      line.append(L - line.length(), ' ');\n      ans.push_back(std::move(line));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of words and a width&nbsp;maxWidth, format the text such that each line has exactly&nbsp;maxWidth&nbsp;characters and is fully (left and right) justified. You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[217,4,504],"class_list":["post-5688","post","type-post","status-publish","format-standard","hentry","category-string","tag-hard","tag-string","tag-text","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5688"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5688\/revisions"}],"predecessor-version":[{"id":5690,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5688\/revisions\/5690"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}