<?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>reconstruct Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/reconstruct/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/reconstruct/</link>
	<description></description>
	<lastBuildDate>Wed, 13 Nov 2019 10:07:15 +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>reconstruct Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/reconstruct/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1253. Reconstruct a 2-Row Binary Matrix</title>
		<link>https://zxi.mytechroad.com/blog/greedy/leetcode-1253-reconstruct-a-2-row-binary-matrix/</link>
					<comments>https://zxi.mytechroad.com/blog/greedy/leetcode-1253-reconstruct-a-2-row-binary-matrix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Nov 2019 06:55:04 +0000</pubDate>
				<category><![CDATA[Greedy]]></category>
		<category><![CDATA[greedy]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[reconstruct]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5820</guid>

					<description><![CDATA[<p>Given the following details of a matrix with&#160;n&#160;columns and&#160;2&#160;rows : The matrix is a binary matrix, which means each element in the matrix can be&#160;0&#160;or&#160;1.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1253-reconstruct-a-2-row-binary-matrix/">花花酱 LeetCode 1253. Reconstruct a 2-Row Binary Matrix</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-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1252 1253 1254 1255 Weekly Contest 162  - 刷题找工作" width="500" height="375" src="https://www.youtube.com/embed/1XMpzhFUvco?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given the following details of a matrix with&nbsp;<code>n</code>&nbsp;columns and&nbsp;<code>2</code>&nbsp;rows :</p>



<ul><li>The matrix is a binary matrix, which means each element in the matrix can be&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li><li>The sum of elements of the 0-th(upper) row is given as&nbsp;<code>upper</code>.</li><li>The sum of elements of the 1-st(lower) row is given as&nbsp;<code>lower</code>.</li><li>The sum of elements in the i-th column(0-indexed) is&nbsp;<code>colsum[i]</code>, where&nbsp;<code>colsum</code>&nbsp;is given as an integer array with length&nbsp;<code>n</code>.</li></ul>



<p>Your task is to reconstruct the matrix with&nbsp;<code>upper</code>,&nbsp;<code>lower</code>&nbsp;and&nbsp;<code>colsum</code>.</p>



<p>Return it as a 2-D integer array.</p>



<p>If there are more than one valid solution, any of them will be accepted.</p>



<p>If no valid solution exists, return an empty 2-D array.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> upper = 2, lower = 1, colsum = [1,1,1]
<strong>Output:</strong> [[1,1,0],[0,0,1]]
<strong>Explanation: </strong>[[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> upper = 2, lower = 3, colsum = [2,2,1,1]
<strong>Output:</strong> []
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
<strong>Output:</strong> [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
</pre>



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



<ul><li><code>1 &lt;= colsum.length &lt;= 10^5</code></li><li><code>0 &lt;= upper, lower &lt;= colsum.length</code></li><li><code>0 &lt;= colsum[i] &lt;= 2</code></li></ul>



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



<p>Two passes:<br>first pass, only process sum = 2, upper = 1, lower = 1<br>second pass, only process sum = 1, whoever has more leftover, assign 1 to that row.</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:
  vector&lt;vector&lt;int&gt;&gt; reconstructMatrix(int upper, int lower, vector&lt;int&gt;&amp; colsum) {
    const int n = colsum.size();
    vector&lt;vector&lt;int&gt;&gt; a(2, vector&lt;int&gt;(n));
    for (int i = 0; i &lt; n; ++i)
      if (colsum[i] == 2) {
        a[0][i] = a[1][i] = 1;
        --upper;
        --lower;
      }
    
    for (int i = 0; i &lt; n; ++i)
      if (colsum[i] == 1)
        if (upper &gt; lower) {
          a[0][i] = 1;
          --upper;
        } else {
          a[1][i] = 1;
          --lower;
        }
    if (upper != 0 || lower != 0) return {};
    return a;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/greedy/leetcode-1253-reconstruct-a-2-row-binary-matrix/">花花酱 LeetCode 1253. Reconstruct a 2-Row Binary Matrix</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-1253-reconstruct-a-2-row-binary-matrix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 23 Aug 2018 08:08:39 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[postorder]]></category>
		<category><![CDATA[preorder]]></category>
		<category><![CDATA[reconstruct]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3675</guid>

					<description><![CDATA[<p>Problem Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7],&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal/">花花酱 LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal</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/53aOi0Drp9I?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Return any binary tree that matches the given preorder and postorder traversals.</p>
<p>Values in the traversals <code>pre</code> and <code>post</code> are distinct positive integers.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>pre = <span id="example-input-1-1">[1,2,4,5,3,6,7]</span>, post = <span id="example-input-1-2">[4,5,2,6,7,3,1]</span>
<strong>Output: </strong><span id="example-output-1">[1,2,3,4,5,6,7]</span>
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= pre.length == post.length &lt;= 30</code></li>
<li><code>pre[]</code> and <code>post[]</code> are both permutations of <code>1, 2, ..., pre.length</code>.</li>
<li>It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.</li>
</ul>
<p><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></p>
<p><img class="alignnone size-full wp-image-3688" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/889-ep219.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/889-ep219.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/889-ep219-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/889-ep219-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution: Recursion</strong></h1>
<p>pre = [(root) (left-child) (right-child)]</p>
<p>post = [(left-child) (right-child) (root)]</p>
<p>We need to recursively find the first node in pre.left-child from post.left-child</p>
<p>e.g.</p>
<p><span id="example-input-1-1">pre = [(<span style="color: #0000ff;"><strong>1</strong></span>), (<span style="color: #ff0000;"><strong>2</strong></span>,4,5), (3,6,7)]</span></p>
<p>post = <span id="example-input-1-2">[(4,5,<strong><span style="color: #ff0000;">2</span></strong>), (6,7,3), (<span style="color: #0000ff;"><strong>1</strong></span>)]</span></p>
<p>First element of left-child is 2 and the length of it is 3.</p>
<p>root = new TreeNode(1)<br />
root.left = build((2,4,5), (4,5,2))<br />
root.right = build((3,6,7), (6,7,3))</p>
<p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  TreeNode* constructFromPrePost(vector&lt;int&gt;&amp; pre, vector&lt;int&gt;&amp; post) {
    return constructFromPrePost(cbegin(pre), cend(pre), cbegin(post), cend(post));
  }
private:
  typedef vector&lt;int&gt;::const_iterator VIT;
  TreeNode* constructFromPrePost(VIT pre_l, VIT pre_r, VIT post_l, VIT post_r) {
    if (pre_l == pre_r) return nullptr;
    TreeNode* root = new TreeNode(*pre_l);
    ++pre_l;
    --post_r;
    if (pre_l == pre_r) return root;
    VIT post_m = next(find(post_l, post_r, *pre_l));
    VIT pre_m = pre_l + (post_m - post_l);
    root-&gt;left = constructFromPrePost(pre_l, pre_m, post_l, post_m);
    root-&gt;right = constructFromPrePost(pre_m, pre_r, post_m, post_r);
    return root;
  }
};</pre><p>Time complexity: O(n^2)</p>
<p>Space complexity: O(n)</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 60 ms
"""
class Solution:
  def constructFromPrePost(self, pre, post):
    def build(i, j, n):
      if n &lt;= 0: return None
      root = TreeNode(pre[i])
      if n == 1: return root
      k = j      
      while post[k] != pre[i + 1]: k += 1
      l = k - j + 1      
      root.left = build(i + 1, j, l)
      root.right = build(i + l + 1, k + 1, n - l - 1)
      return root
    return build(0, 0, len(pre))</pre><p>Time complexity: O(n)</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 52 ms (beats 100%)
"""
class Solution:
  def constructFromPrePost(self, pre, post):    
    def build(i, j, n):
      if n &lt;= 0: return None
      root = TreeNode(pre[i])
      if n == 1: return root
      k = index[pre[i + 1]]      
      l = k - j + 1      
      root.left = build(i + 1, j, l)
      root.right = build(i + l + 1, k + 1, n - l - 1)
      return root
    index = {}
    for i in range(len(pre)):
      index[post[i]] = i
    return build(0, 0, len(pre))</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-889-construct-binary-tree-from-preorder-and-postorder-traversal/">花花酱 LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal</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-889-construct-binary-tree-from-preorder-and-postorder-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
