<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>printer Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/printer/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/printer/</link>
	<description></description>
	<lastBuildDate>Sat, 08 Sep 2018 20:36:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>printer Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/printer/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 664. Strange Printer</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-664-strange-printer/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-664-strange-printer/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 21 Sep 2017 06:15:24 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(n^3)]]></category>
		<category><![CDATA[printer]]></category>
		<category><![CDATA[sequence]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=379</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-664-strange-printer/">花花酱 LeetCode 664. Strange Printer</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/YQQUGsb7mww?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>There is a strange printer with the following two special requirements:</p>
<ol>
<li>The printer can only print a sequence of the same character each time.</li>
<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>
</ol>
<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>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: &quot;aaabbb&quot;
Output: 2
Explanation: Print &quot;aaa&quot; first and then print &quot;bbb&quot;.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: &quot;aba&quot;
Output: 2
Explanation: Print &quot;aaa&quot; first and then print &quot;b&quot; from the second place of the string, which will cover the existing character 'a'.</pre><p><b>Hint</b>: Length of the given string will not exceed 100.</p>
<p><strong>Idea:</strong></p>
<p>Dynamic programming</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/664-ep66.png"><img 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="(max-width: 960px) 100vw, 960px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<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 />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Time Complexity: </strong></p>
<p>O(n^3)</p>
<p><strong>Space Complexity:</strong></p>
<p>O(n^2)</p>
<p><strong>Solution:</strong></p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time Complexity: O(n^3)
// Space Complexity: O(n^2)
// Running Time: 22 ms
class Solution {
public:
    int strangePrinter(const string&amp; s) {
        int l = s.length();
        t_ = vector&lt;vector&lt;int&gt;&gt;(l, vector&lt;int&gt;(l, 0));
        return turn(s, 0, s.length() - 1);
    }
private:
    // Minimal turns to print s[i] to s[j] 
    int turn(const string&amp; s, int i, int j) {
        // Empty string
        if (i &gt; j) return 0;        
        // Solved
        if (t_[i][j] &gt; 0) return t_[i][j];
        
        // Default behavior, print s[i] to s[j-1] and print s[j]
        int ans = turn(s, i, j-1) + 1;
        
        for (int k = i; k &lt; j; ++k)
            if (s[k] == s[j])
                ans = min(ans, turn(s, i, k) + turn(s, k + 1, j - 1));
        
        return t_[i][j] = ans;
    }
    
    vector&lt;vector&lt;int&gt;&gt; t_;
    
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time Complexity: O(n^3)
// Space Complexity: O(n^2)
// Running Time: 31 ms (beat 99.25%) 9/2017
class Solution {
    public int strangePrinter(String s) {
        int l = s.length();
        t_ = new int[l][l];
        return turn(s.toCharArray(), 0, l - 1);
    }
    
    // Minimal turns to print s[i] to s[j] 
    private int turn(char[] s, int i, int j) {
        // Empty string
        if (i &gt; j) return 0;        
        // Solved
        if (t_[i][j] &gt; 0) return t_[i][j];
        // Default behavior, print s[i] to s[j-1] and print s[j]
        int ans = turn(s, i, j-1) + 1;        
        for (int k = i; k &lt; j; ++k)
            if (s[k] == s[j])
                ans = Math.min(ans, turn(s, i, k) + turn(s, k + 1, j - 1));
        
        return t_[i][j] = ans;
    }
        
    private int[][] t_;    
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Time Complexity: O(n^3)
Space Complexity: O(n^2)
Running Time: 895 ms (beat 66%) 9/2017
"""
class Solution(object):
    def strangePrinter(self, s):
        """
        :type s: str
        :rtype: int
        """
        l = len(s)
        self._t = [[0 for _ in xrange(l)] for _ in xrange(l)]
        
        return self._turns(s, 0, l - 1)
    
    def _turns(self, s, i, j):
        if i &gt; j: return 0
        if self._t[i][j] &gt; 0: return self._t[i][j]
        
        ans = self._turns(s, i, j - 1) + 1
        
        for k in xrange(i, j):
            if s[k] == s[j]:
                ans = min(ans, self._turns(s, i, k) 
                             + self._turns(s, k + 1, j-1))
        self._t[i][j] = ans
        
        return self._t[i][j]</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-664-strange-printer/">花花酱 LeetCode 664. Strange Printer</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-664-strange-printer/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
