<?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>hash Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hash/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hash/</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2020 23:18:38 +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>hash Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hash/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Consistent Hashing</title>
		<link>https://zxi.mytechroad.com/blog/cs/consistent-hashing/</link>
					<comments>https://zxi.mytechroad.com/blog/cs/consistent-hashing/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 30 Dec 2020 23:18:37 +0000</pubDate>
				<category><![CDATA[CS]]></category>
		<category><![CDATA[consistent hashing]]></category>
		<category><![CDATA[distributed system]]></category>
		<category><![CDATA[hash]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7879</guid>

					<description><![CDATA[<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/cs/consistent-hashing/">Consistent Hashing</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 Consistent Hashing - CS大讲堂 EP17" width="500" height="281" src="https://www.youtube.com/embed/lm6Zeo3tqK4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-1.png" alt="" class="wp-image-7880" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-2.png" alt="" class="wp-image-7881" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-3.png" alt="" class="wp-image-7882" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-4.png" alt="" class="wp-image-7883" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-5.png" alt="" class="wp-image-7884" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-5.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-5-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-5-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-6.png" alt="" class="wp-image-7886" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-6.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-6-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/12/consistent-hashing-ep17-6-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/cs/consistent-hashing/">Consistent Hashing</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/cs/consistent-hashing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1316. Distinct Echo Substrings</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 11 Jan 2020 17:34:57 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[hashset]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6065</guid>

					<description><![CDATA[<p>Return the number of&#160;distinct&#160;non-empty substrings of&#160;text&#160;that can be written as the concatenation of some string with itself. Example 1: Input: text = "abcabcabc" Output: 3&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/">花花酱 LeetCode 1316. Distinct Echo Substrings</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>Return the number of&nbsp;<strong>distinct</strong>&nbsp;non-empty substrings of&nbsp;<code>text</code>&nbsp;that can be written as the concatenation of some string with itself.</p>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> text = "abcabcabc"
<strong>Output:</strong> 3
<strong>Explanation: </strong>The 3 substrings are "abcabc", "bcabca" and "cabcab".
</pre>



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



<pre class="wp-block-preformatted; crayon:false"><strong>Input:</strong> text = "leetcodeleetcode"
<strong>Output:</strong> 2
<strong>Explanation: </strong>The 2 substrings are "ee" and "leetcodeleetcode".
</pre>



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



<ul><li><code>1 &lt;= text.length &lt;= 2000</code></li><li><code>text</code>&nbsp;has only lowercase English letters.</li></ul>



<h2><strong>Solution 1:  Brute Force + HashSet</strong></h2>



<p>Try all possible substrings</p>



<p>Time complexity: O(n^3)<br>Space complexity: O(n^2)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int distinctEchoSubstrings(string text) {
    const int n = text.length();
    string_view t(text);
    unordered_set&lt;string_view&gt; s;    
    for (int k = 1; k &lt;= n / 2; ++k)
      for (int i = 0; i + k &lt;= n; ++i)
        if (t.substr(i, k) == t.substr(i + k, k))
          s.insert(t.substr(i, 2 * k));
    return s.size();
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1316-distinct-echo-substrings/">花花酱 LeetCode 1316. Distinct Echo Substrings</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/string/leetcode-1316-distinct-echo-substrings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 652. Find Duplicate Subtrees</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-652-find-duplicate-subtrees/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-652-find-duplicate-subtrees/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 Dec 2017 23:18:34 +0000</pubDate>
				<category><![CDATA[Medium]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[subtree]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1390</guid>

					<description><![CDATA[<p>652.&#160;Find Duplicate SubtreesMedium730151FavoriteShare Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-652-find-duplicate-subtrees/">花花酱 LeetCode 652. Find Duplicate Subtrees</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>

652.&nbsp;Find Duplicate SubtreesMedium730151FavoriteShare</p>



<p>Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any&nbsp;<strong>one</strong>&nbsp;of them.</p>



<p>Two trees are duplicate if they have the same structure with same node values.</p>



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



<p>The following are two duplicate subtrees:</p>



<pre class="wp-block-preformatted; crayon:false">        1
       / \
      2   3
     /   / \
    4   2   4
       /
      4</pre>



<p>  2<br> /<br>4</p>



<p>and</p>



<p>4</p>



<p>Therefore, you need to return above trees&#8217; root in the form of a list.

</p>



<figure class="wp-block-image"><img src="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/652-ep146-1.png" alt=""/></figure>



<h2><strong>Solution 1: Serialization </strong></h2>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 29 ms
class Solution {
public:
    vector&lt;TreeNode*&gt; findDuplicateSubtrees(TreeNode* root) {
        unordered_map&lt;string, int&gt; counts;
        vector&lt;TreeNode*&gt; ans;
        serialize(root, counts, ans);
        return ans;
    }
private:
    string serialize(TreeNode* root, unordered_map&lt;string, int&gt;&amp; counts, vector&lt;TreeNode*&gt;&amp; ans) {
        if (!root) return &quot;#&quot;;
        string key = to_string(root-&gt;val) + &quot;,&quot; 
                     + serialize(root-&gt;left, counts, ans) + &quot;,&quot; 
                     + serialize(root-&gt;right, counts, ans);
        if (++counts[key] == 2)
            ans.push_back(root);
        return key;
    }
};</pre>
</div></div>



<p><strong>Solution 2: int id for each unique subtree</strong></p>



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



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

<pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 8 ms
class Solution {
public:
  vector&lt;TreeNode*&gt; findDuplicateSubtrees(TreeNode* root) {
    unordered_map&lt;long, pair&lt;int,int&gt;&gt; counts;    
    vector&lt;TreeNode*&gt; ans;
    getId(root, counts, ans);
    return ans;
  }
private:
  int getId(TreeNode* root, 
            unordered_map&lt;long, pair&lt;int,int&gt;&gt;&amp; counts,
            vector&lt;TreeNode*&gt;&amp; ans) {
    if (!root) return 0;
    long key = (static_cast&lt;long&gt;(static_cast&lt;unsigned&gt;(root-&gt;val)) &lt;&lt; 32) +
               (getId(root-&gt;left, counts, ans) &lt;&lt; 16) +
                getId(root-&gt;right, counts, ans);    
    auto&amp; p = counts[key];
    if (p.second++ == 0)
      p.first = counts.size();    
    else if (p.second == 2)
      ans.push_back(root);
    return p.first;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-652-find-duplicate-subtrees/">花花酱 LeetCode 652. Find Duplicate Subtrees</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/tree/leetcode-652-find-duplicate-subtrees/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 690. Employee Importance</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-690-employee-importance/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-690-employee-importance/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 29 Sep 2017 05:17:25 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[hash]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=458</guid>

					<description><![CDATA[<p>https://leetcode.com/problems/employee-importance/description/ Problem: ou are given a data structure of employee information, which includes the employee&#8217;s unique id, his importance value and his direct subordinates&#8217; id. For example, employee 1 is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-690-employee-importance/">花花酱 LeetCode 690. Employee Importance</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/kK9TBtQBmCg?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><a href="https://leetcode.com/problems/employee-importance/description/">https://leetcode.com/problems/employee-importance/description/</a></p>
<p><strong>Problem:</strong></p>
<p>ou are given a data structure of employee information, which includes the employee&#8217;s <b>unique id</b>, his <b>importance value</b> and his <b>direct</b> subordinates&#8217; id.</p>
<p>For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is <b>not direct</b>.</p>
<p>Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
Explanation:
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. 
So the total importance value of employee 1 is 5 + 3 + 3 = 11.</pre><p><b>Note:</b></p>
<ol>
<li>One employee has at most one <b>direct</b> leader and may have several subordinates.</li>
<li>The maximum number of employees won&#8217;t exceed 2000.</li>
</ol>
<p><strong>Idea:</strong></p>
<p>BFS / DFS</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75.png"><img class="alignnone size-full wp-image-466" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/690-ep75-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:</strong></p>
<p>C++ / BFS</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 13 ms
class Solution {
public:
    int getImportance(vector&lt;Employee*&gt; employees, int id) {
        unordered_map&lt;int, Employee*&gt; es;        
        for (auto e : employees)
            es.emplace(e-&gt;id, e);                
        
        queue&lt;int&gt; q;
        q.push(id);
        int sum = 0;
        while (!q.empty()) {
            int eid = q.front(); q.pop();
            auto e = es[eid];
            sum += e-&gt;importance;
            for (auto rid : e-&gt;subordinates)
                q.push(rid);
        }
        return sum;
    }
};</pre><p>&nbsp;</p>
<p>C++ / DFS</p><pre class="crayon-plain-tag">class Solution {
public:
    int getImportance(vector&lt;Employee*&gt; employees, int id) {
        unordered_map&lt;int, Employee*&gt; es;
        for (auto e : employees)
            es.emplace(e-&gt;id, e);
        return dfs(id, es);
    }
private:
    int dfs(int id, const unordered_map&lt;int, Employee*&gt;&amp; es) {
        const auto e = es.at(id);
        int sum = e-&gt;importance;
        for (int rid : e-&gt;subordinates)
            sum += dfs(rid, es);
        return sum;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-690-employee-importance/">花花酱 LeetCode 690. Employee Importance</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/searching/leetcode-690-employee-importance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
