{"id":379,"date":"2017-09-20T23:15:24","date_gmt":"2017-09-21T06:15:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=379"},"modified":"2018-09-08T13:36:43","modified_gmt":"2018-09-08T20:36:43","slug":"leetcode-664-strange-printer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-664-strange-printer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 664. Strange Printer"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 664. Strange Printer - \u5237\u9898\u627e\u5de5\u4f5c EP66\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/YQQUGsb7mww?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>There is a strange printer with the following two special requirements:<\/p>\n<ol>\n<li>The printer can only print a sequence of the same character each time.<\/li>\n<li>At each turn, the printer can print new characters starting from and ending at any places, and will cover the original existing characters.<\/li>\n<\/ol>\n<p>Given a string consists of lower English letters only, your job is to count the minimum number of turns the printer needed in order to print it.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"aaabbb\"\r\nOutput: 2\r\nExplanation: Print \"aaa\" first and then print \"bbb\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"aba\"\r\nOutput: 2\r\nExplanation: Print \"aaa\" first and then print \"b\" from the second place of the string, which will cover the existing character 'a'.\r\n<\/pre>\n<p><b>Hint<\/b>: Length of the given string will not exceed 100.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Dynamic programming<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-380\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/664-ep66-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Time Complexity:\u00a0<\/strong><\/p>\n<p>O(n^3)<\/p>\n<p><strong>Space Complexity:<\/strong><\/p>\n<p>O(n^2)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Time Complexity: O(n^3)\r\n\/\/ Space Complexity: O(n^2)\r\n\/\/ Running Time: 22 ms\r\nclass Solution {\r\npublic:\r\n    int strangePrinter(const string&amp; s) {\r\n        int l = s.length();\r\n        t_ = vector&lt;vector&lt;int&gt;&gt;(l, vector&lt;int&gt;(l, 0));\r\n        return turn(s, 0, s.length() - 1);\r\n    }\r\nprivate:\r\n    \/\/ Minimal turns to print s[i] to s[j] \r\n    int turn(const string&amp; s, int i, int j) {\r\n        \/\/ Empty string\r\n        if (i &gt; j) return 0;        \r\n        \/\/ Solved\r\n        if (t_[i][j] &gt; 0) return t_[i][j];\r\n        \r\n        \/\/ Default behavior, print s[i] to s[j-1] and print s[j]\r\n        int ans = turn(s, i, j-1) + 1;\r\n        \r\n        for (int k = i; k &lt; j; ++k)\r\n            if (s[k] == s[j])\r\n                ans = min(ans, turn(s, i, k) + turn(s, k + 1, j - 1));\r\n        \r\n        return t_[i][j] = ans;\r\n    }\r\n    \r\n    vector&lt;vector&lt;int&gt;&gt; t_;\r\n    \r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Time Complexity: O(n^3)\r\n\/\/ Space Complexity: O(n^2)\r\n\/\/ Running Time: 31 ms (beat 99.25%) 9\/2017\r\nclass Solution {\r\n    public int strangePrinter(String s) {\r\n        int l = s.length();\r\n        t_ = new int[l][l];\r\n        return turn(s.toCharArray(), 0, l - 1);\r\n    }\r\n    \r\n    \/\/ Minimal turns to print s[i] to s[j] \r\n    private int turn(char[] s, int i, int j) {\r\n        \/\/ Empty string\r\n        if (i &gt; j) return 0;        \r\n        \/\/ Solved\r\n        if (t_[i][j] &gt; 0) return t_[i][j];\r\n        \/\/ Default behavior, print s[i] to s[j-1] and print s[j]\r\n        int ans = turn(s, i, j-1) + 1;        \r\n        for (int k = i; k &lt; j; ++k)\r\n            if (s[k] == s[j])\r\n                ans = Math.min(ans, turn(s, i, k) + turn(s, k + 1, j - 1));\r\n        \r\n        return t_[i][j] = ans;\r\n    }\r\n        \r\n    private int[][] t_;    \r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nTime Complexity: O(n^3)\r\nSpace Complexity: O(n^2)\r\nRunning Time: 895 ms (beat 66%) 9\/2017\r\n\"\"\"\r\nclass Solution(object):\r\n    def strangePrinter(self, s):\r\n        \"\"\"\r\n        :type s: str\r\n        :rtype: int\r\n        \"\"\"\r\n        l = len(s)\r\n        self._t = [[0 for _ in xrange(l)] for _ in xrange(l)]\r\n        \r\n        return self._turns(s, 0, l - 1)\r\n    \r\n    def _turns(self, s, i, j):\r\n        if i &gt; j: return 0\r\n        if self._t[i][j] &gt; 0: return self._t[i][j]\r\n        \r\n        ans = self._turns(s, i, j - 1) + 1\r\n        \r\n        for k in xrange(i, j):\r\n            if s[k] == s[j]:\r\n                ans = min(ans, self._turns(s, i, k) \r\n                             + self._turns(s, k + 1, j-1))\r\n        self._t[i][j] = ans\r\n        \r\n        return self._t[i][j]\r\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem: There is a strange printer with the following two special requirements: The printer can only print a sequence of the same character each time.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,217,398,99,100],"class_list":["post-379","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-on3","tag-printer","tag-sequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/379","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=379"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":3896,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/379\/revisions\/3896"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}