{"id":2381,"date":"2018-03-24T20:01:00","date_gmt":"2018-03-25T03:01:00","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2381"},"modified":"2018-03-24T20:01:43","modified_gmt":"2018-03-25T03:01:43","slug":"leetcode-806-number-of-lines-to-write-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-806-number-of-lines-to-write-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 806. Number of Lines To Write String"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u4e2a\u5b57\u6bcd\u7684\u5bbd\u5ea6\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u95ee\u4e00\u5171\u9700\u8981\u591a\u5c11\u884c\u548c\u591a\u5c11\u5217\u624d\u80fd\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6253\u5370\u51fa\u6765\u3002\u5c06\u8bbe\u6bcf\u884c\u7684\u5bbd\u5ea6\u662f100\u3002<\/p>\n<p>We are to write the letters of a given string\u00a0<code>S<\/code>, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array\u00a0<code>widths<\/code>, an array where widths[0] is the width of &#8216;a&#8217;, widths[1] is the width of &#8216;b&#8217;, &#8230;, and widths[25] is the width of &#8216;z&#8217;.<\/p>\n<p>Now answer two questions: how many lines have at least one character from\u00a0<code>S<\/code>, and what is the width used by the last such line? Return your answer as an integer list of length 2.<\/p>\n<pre class=\"crayon:false\"><strong>Example :<\/strong>\r\n<strong>Input:<\/strong> \r\nwidths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\r\nS = \"abcdefghijklmnopqrstuvwxyz\"\r\n<strong>Output:<\/strong> [3, 60]\r\n<strong>Explanation: <\/strong>\r\nAll letters have the same length of 10. To write all 26 letters,\r\nwe need two full lines and one line with 60 units.\r\n<\/pre>\n<pre class=\"crayon:false \"><strong>Example :<\/strong>\r\n<strong>Input:<\/strong> \r\nwidths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]\r\nS = \"bbbcccdddaaa\"\r\n<strong>Output:<\/strong> [2, 4]\r\n<strong>Explanation: <\/strong>\r\nAll letters except 'a' have the same length of 10, and \r\n\"bbbcccdddaa\" will cover 9 * 10 + 2 * 4 = 98 units.\r\nFor the last 'a', it is written on the second line because\r\nthere is only 2 units left in the first line.\r\nSo the answer is 2 lines, plus 4 units in the second line.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The length of\u00a0<code>S<\/code>\u00a0will be in the range\u00a0[1, 1000].<\/li>\n<li><code>S<\/code>\u00a0will only contain lowercase letters.<\/li>\n<li><code>widths<\/code>\u00a0is\u00a0an array of length\u00a0<code>26<\/code>.<\/li>\n<li><code>widths[i]<\/code>\u00a0will be in the range of\u00a0<code>[2, 10]<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Time Complexity: O(n)<\/p>\n<p>Space Complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; numberOfLines(vector&lt;int&gt;&amp; widths, string S) {\r\n    int lines = 1;\r\n    int units = 0;\r\n    for (char c : S) {\r\n      if (units + widths[c - 'a'] &gt; 100) {\r\n        ++lines;\r\n        units = 0;\r\n      }\r\n      units += widths[c - 'a'];\r\n    }\r\n    return {lines, units};\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u6bcf\u4e2a\u5b57\u6bcd\u7684\u5bbd\u5ea6\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u95ee\u4e00\u5171\u9700\u8981\u591a\u5c11\u884c\u548c\u591a\u5c11\u5217\u624d\u80fd\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6253\u5370\u51fa\u6765\u3002\u5c06\u8bbe\u6bcf\u884c\u7684\u5bbd\u5ea6\u662f100\u3002 We are to write the letters of a given string\u00a0S, from left to right into lines. Each line has maximum width 100 units,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127,47],"tags":[222,4],"class_list":["post-2381","post","type-post","status-publish","format-standard","hentry","category-geometry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2381","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=2381"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions"}],"predecessor-version":[{"id":2384,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions\/2384"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}