<?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>construction Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/construction/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/construction/</link>
	<description></description>
	<lastBuildDate>Sun, 31 Jan 2021 04:53:47 +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>construction Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/construction/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1743. Restore the Array From Adjacent Pairs</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1743-restore-the-array-from-adjacent-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1743-restore-the-array-from-adjacent-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 Jan 2021 04:53:18 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[construction]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8049</guid>

					<description><![CDATA[<p>There is an integer array&#160;nums&#160;that consists of&#160;n&#160;unique&#160;elements, but you have forgotten it. However, you do remember every pair of adjacent elements in&#160;nums. You are given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1743-restore-the-array-from-adjacent-pairs/">花花酱 LeetCode 1743. Restore the Array From Adjacent Pairs</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>There is an integer array&nbsp;<code>nums</code>&nbsp;that consists of&nbsp;<code>n</code>&nbsp;<strong>unique&nbsp;</strong>elements, but you have forgotten it. However, you do remember every pair of adjacent elements in&nbsp;<code>nums</code>.</p>



<p>You are given a 2D integer array&nbsp;<code>adjacentPairs</code>&nbsp;of size&nbsp;<code>n - 1</code>&nbsp;where each&nbsp;<code>adjacentPairs[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>&nbsp;indicates that the elements&nbsp;<code>u<sub>i</sub></code>&nbsp;and&nbsp;<code>v<sub>i</sub></code>&nbsp;are adjacent in&nbsp;<code>nums</code>.</p>



<p>It is guaranteed that every adjacent pair of elements&nbsp;<code>nums[i]</code>&nbsp;and&nbsp;<code>nums[i+1]</code>&nbsp;will exist in&nbsp;<code>adjacentPairs</code>, either as&nbsp;<code>[nums[i], nums[i+1]]</code>&nbsp;or&nbsp;<code>[nums[i+1], nums[i]]</code>. The pairs can appear&nbsp;<strong>in any order</strong>.</p>



<p>Return&nbsp;<em>the original array&nbsp;</em><code>nums</code><em>. If there are multiple solutions, return&nbsp;<strong>any of them</strong></em>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> adjacentPairs = [[2,1],[3,4],[3,2]]
<strong>Output:</strong> [1,2,3,4]
<strong>Explanation:</strong> This array has all its adjacent pairs in adjacentPairs.
Notice that adjacentPairs[i] may not be in left-to-right order.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> adjacentPairs = [[4,-2],[1,4],[-3,1]]
<strong>Output:</strong> [-2,4,1,-3]
<strong>Explanation:</strong> There can be negative numbers.
Another solution is [-3,1,4,-2], which would also be accepted.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> adjacentPairs = [[100000,-100000]]
<strong>Output:</strong> [100000,-100000]
</pre>



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



<ul><li><code>nums.length == n</code></li><li><code>adjacentPairs.length == n - 1</code></li><li><code>adjacentPairs[i].length == 2</code></li><li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>-10<sup>5</sup>&nbsp;&lt;= nums[i], u<sub>i</sub>, v<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li><li>There exists some&nbsp;<code>nums</code>&nbsp;that has&nbsp;<code>adjacentPairs</code>&nbsp;as its pairs.</li></ul>



<h2><strong>Solution: Hashtable </strong></h2>



<p>Reverse thinking! For a given input array, e.g. <br>[1, 2, 3, 4, 5]<br>it&#8217;s adjacent pairs are [1,2] , [2,3], [3,4], [4,5]<br>all numbers appeared exactly twice except 1 and 5, since they are on the boundary. <br>We just need to find the head or tail of the input array, and construct the rest of the array in order.</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
class Solution {
public:
  vector&lt;int&gt; restoreArray(vector&lt;vector&lt;int&gt;&gt;&amp; adjacentPairs) {
    const int n = adjacentPairs.size() + 1;
    unordered_map&lt;int, vector&lt;int&gt;&gt; g;
    for (const auto&amp; p : adjacentPairs) {
      g[p[0]].push_back(p[1]);
      g[p[1]].push_back(p[0]);
    }
    
    vector&lt;int&gt; ans(n);
    for (const auto&amp; [u, vs] : g)
      if (vs.size() == 1) {
        ans[0] = u;
        ans[1] = vs[0];
        break;
      }
        
    for (int i = 2; i &lt; n; ++i) {
      const auto&amp; vs = g[ans[i - 1]];
      ans[i] = vs[0] == ans[i - 2] ? vs[1] : vs[0];      
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1743-restore-the-array-from-adjacent-pairs/">花花酱 LeetCode 1743. Restore the Array From Adjacent Pairs</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-1743-restore-the-array-from-adjacent-pairs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1405. Longest Happy String</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1405-longest-happy-string/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1405-longest-happy-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 08:23:13 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[construction]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6581</guid>

					<description><![CDATA[<p>A string is called&#160;happy&#160;if it does&#160;not have any of the strings&#160;'aaa',&#160;'bbb'&#160;or&#160;'ccc'&#160;as a substring. Given three integers&#160;a,&#160;b&#160;and&#160;c, return&#160;any&#160;string&#160;s,&#160;which satisfies following conditions: s&#160;is&#160;happy&#160;and longest possible. s&#160;contains&#160;at most&#160;a&#160;occurrences&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1405-longest-happy-string/">花花酱 LeetCode 1405. Longest Happy String</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>A string is called&nbsp;<em>happy</em>&nbsp;if it does&nbsp;not have any of the strings&nbsp;<code>'aaa'</code>,&nbsp;<code>'bbb'</code>&nbsp;or&nbsp;<code>'ccc'</code>&nbsp;as a substring.</p>



<p>Given three integers&nbsp;<code>a</code>,&nbsp;<code>b</code>&nbsp;and&nbsp;<code>c</code>, return&nbsp;<strong>any</strong>&nbsp;string&nbsp;<code>s</code>,&nbsp;which satisfies following conditions:</p>



<ul><li><code>s</code>&nbsp;is&nbsp;<em>happy&nbsp;</em>and longest possible.</li><li><code>s</code>&nbsp;contains&nbsp;<strong>at most</strong>&nbsp;<code>a</code>&nbsp;occurrences of the letter&nbsp;<code>'a'</code>,&nbsp;<strong>at most</strong>&nbsp;<code>b</code>&nbsp;occurrences of the letter&nbsp;<code>'b'</code>&nbsp;and&nbsp;<strong>at most</strong>&nbsp;<code>c</code>&nbsp;occurrences of the letter&nbsp;<code>'c'</code>.</li><li><code>s&nbsp;</code>will only contain&nbsp;<code>'a'</code>,&nbsp;<code>'b'</code>&nbsp;and&nbsp;<code>'c'</code>&nbsp;letters.</li></ul>



<p>If there is no such string&nbsp;<code>s</code>&nbsp;return the empty string&nbsp;<code>""</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> a = 1, b = 1, c = 7
<strong>Output:</strong> "ccaccbcc"
<strong>Explanation:</strong> "ccbccacc" would also be a correct answer.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> a = 2, b = 2, c = 1
<strong>Output:</strong> "aabbc"
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> a = 7, b = 1, c = 0
<strong>Output:</strong> "aabaa"
<strong>Explanation:</strong> It's the only correct answer in this case.
</pre>



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



<ul><li><code>0 &lt;= a, b, c &lt;= 100</code></li><li><code>a + b + c &gt; 0</code></li></ul>



<h2><strong>Solution: Greedy</strong></h2>



<p>Put the char with highest frequency first if its consecutive length of that char is &lt; 2 <br>or put one char if any of other two chars has consecutive length of 2.</p>



<p>increase the consecutive length of itself and reset that for other two chars.</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:
  string longestDiverseString(int a, int b, int c) {
    const int total = a + b + c;        
    vector&lt;int&gt; f{a, b, c};
    vector&lt;int&gt; l{0, 0, 0};
    string ans;
    for (int _ = 0; _ &lt; total; ++_)
      for (int i = 0; i &lt; 3; ++i) {
        const int j = (i + 1) % 3;
        const int k = (i + 2) % 3;        
        if ((f[i] &gt;= f[j] &amp;&amp; f[i] &gt;= f[k] &amp;&amp; l[i] != 2) 
            || (f[i] &gt; 0 &amp;&amp; (l[j] == 2 || l[k] == 2))) {
          ans += 'a' + i;
          ++l[i];
          --f[i];
          l[j] = l[k] = 0;
          break;
      }    
    }
    return ans;
  }
};</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1405-longest-happy-string/">花花酱 LeetCode 1405. Longest Happy String</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/greedy/leetcode-1405-longest-happy-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
