<?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>sequence Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/sequence/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/sequence/</link>
	<description></description>
	<lastBuildDate>Tue, 01 Feb 2022 07:29:55 +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>sequence Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/sequence/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2145. Count the Hidden Sequences</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2145-count-the-hidden-sequences/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2145-count-the-hidden-sequences/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 01 Feb 2022 07:29:44 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[sequence]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9454</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;array of&#160;n&#160;integers&#160;differences, which describes the&#160;differences&#160;between each pair of&#160;consecutive&#160;integers of a&#160;hidden&#160;sequence of length&#160;(n + 1). More formally, call the hidden sequence&#160;hidden, then we&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2145-count-the-hidden-sequences/">花花酱 LeetCode 2145. Count the Hidden Sequences</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;array of&nbsp;<code>n</code>&nbsp;integers&nbsp;<code>differences</code>, which describes the&nbsp;<strong>differences&nbsp;</strong>between each pair of&nbsp;<strong>consecutive&nbsp;</strong>integers of a&nbsp;<strong>hidden</strong>&nbsp;sequence of length&nbsp;<code>(n + 1)</code>. More formally, call the hidden sequence&nbsp;<code>hidden</code>, then we have that&nbsp;<code>differences[i] = hidden[i + 1] - hidden[i]</code>.</p>



<p>You are further given two integers&nbsp;<code>lower</code>&nbsp;and&nbsp;<code>upper</code>&nbsp;that describe the&nbsp;<strong>inclusive</strong>&nbsp;range of values&nbsp;<code>[lower, upper]</code>&nbsp;that the hidden sequence can contain.</p>



<ul><li>For example, given&nbsp;<code>differences = [1, -3, 4]</code>,&nbsp;<code>lower = 1</code>,&nbsp;<code>upper = 6</code>, the hidden sequence is a sequence of length&nbsp;<code>4</code>&nbsp;whose elements are in between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>6</code>&nbsp;(<strong>inclusive</strong>).<ul><li><code>[3, 4, 1, 5]</code>&nbsp;and&nbsp;<code>[4, 5, 2, 6]</code>&nbsp;are possible hidden sequences.</li><li><code>[5, 6, 3, 7]</code>&nbsp;is not possible since it contains an element greater than&nbsp;<code>6</code>.</li><li><code>[1, 2, 3, 4]</code>&nbsp;is not possible since the differences are not correct.</li></ul></li></ul>



<p>Return&nbsp;<em>the number of&nbsp;<strong>possible</strong>&nbsp;hidden sequences there are.</em>&nbsp;If there are no possible sequences, return&nbsp;<code>0</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> differences = [1,-3,4], lower = 1, upper = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> The possible hidden sequences are:
- [3, 4, 1, 5]
- [4, 5, 2, 6]
Thus, we return 2.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> differences = [3,-4,5,1,-2], lower = -4, upper = 5
<strong>Output:</strong> 4
<strong>Explanation:</strong> The possible hidden sequences are:
- [-3, 0, -4, 1, 2, 0]
- [-2, 1, -3, 2, 3, 1]
- [-1, 2, -2, 3, 4, 2]
- [0, 3, -1, 4, 5, 3]
Thus, we return 4.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> differences = [4,-7,2], lower = 3, upper = 6
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no possible hidden sequences. Thus, we return 0.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>n == differences.length</code></li><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>-10<sup>5</sup>&nbsp;&lt;= differences[i] &lt;= 10<sup>5</sup></code></li><li><code>-10<sup>5</sup>&nbsp;&lt;= lower &lt;= upper &lt;= 10<sup>5</sup></code></li></ul>



<h2><strong>Solution: Math</strong></h2>



<p>Find the min and max of the cumulative sum of the differences.</p>



<p>Ans = max(0, upper &#8211; lower &#8211; (hi &#8211; lo) + 1)</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numberOfArrays(vector&lt;int&gt;&amp; differences, int lower, int upper) {
    long long s = 0;
    long long hi = 0;
    long long lo = 0;
    for (int d : differences) {
      s += d;
      hi = max(hi, s);
      lo = min(lo, s);
    }
    return max(0LL, upper - lower - (hi - lo) + 1);
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2145-count-the-hidden-sequences/">花花酱 LeetCode 2145. Count the Hidden Sequences</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/math/leetcode-2145-count-the-hidden-sequences/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 128. Longest Consecutive Sequence</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-128-longest-consecutive-sequence/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-128-longest-consecutive-sequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 03 Oct 2017 06:43:40 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[longest]]></category>
		<category><![CDATA[sequence]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=524</guid>

					<description><![CDATA[<p>&#160; Problem: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2],&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-128-longest-consecutive-sequence/">花花酱 LeetCode 128. Longest Consecutive Sequence</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/rc2QdQ7U78I?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>&nbsp;</p>
<p><strong>Problem:</strong></p>
<p>Given an unsorted array of integers, find the length of the longest consecutive elements sequence.</p>
<p>For example,<br />
Given <code>[100, 4, 200, 1, 3, 2]</code>,<br />
The longest consecutive elements sequence is <code>[1, 2, 3, 4]</code>. Return its length: <code>4</code>.</p>
<p>Your algorithm should run in O(<i>n</i>) complexity.</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>Idea:</strong></p>
<p>Hashtable / Hashset</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1.png"><img class="alignnone size-full wp-image-528" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>&nbsp;</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2.png"><img class="alignnone size-full wp-image-527" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/128-ep80-2-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Time complexity: O(n)</strong></p>
<p><strong>Space complexity: O(n)</strong></p>
<p><strong>Solution 1: </strong>C++ / online</p><pre class="crayon-plain-tag">class Solution {
public:
    int longestConsecutive(vector&lt;int&gt;&amp; nums) {
        unordered_map&lt;int, int&gt; h;
        int ans = 0;
        for (int num : nums) {
            if (h.count(num)) continue;
            
            auto it_l = h.find(num - 1);
            auto it_r = h.find(num + 1);
            
            int l = it_l != h.end() ? it_l-&gt;second : 0;
            int r = it_r != h.end() ? it_r-&gt;second : 0;
            int t = l + r + 1;
            
            h[num] = h[num - l] = h[num + r] = t;
            
            ans = max(ans, t);            
        }
        
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>Solution 2: C++ / offline</p><pre class="crayon-plain-tag">class Solution {
public:
    int longestConsecutive(vector&lt;int&gt;&amp; nums) {
        unordered_set&lt;int&gt; h(nums.begin(), nums.end());
        int ans = 0;
        for (int num : nums)            
            if (!h.count(num - 1)) {
                int l = 0;
                while (h.count(num++)) ++l;
                ans = max(ans, l);
            }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-128-longest-consecutive-sequence/">花花酱 LeetCode 128. Longest Consecutive Sequence</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/hashtable/leetcode-128-longest-consecutive-sequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
