<?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>backwards Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/backwards/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/backwards/</link>
	<description></description>
	<lastBuildDate>Sat, 22 Feb 2020 08:19:24 +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>backwards Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/backwards/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1354. Construct Target Array With Multiple Sums</title>
		<link>https://zxi.mytechroad.com/blog/simulation/eetcode-1354-construct-target-array-with-multiple-sums/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/eetcode-1354-construct-target-array-with-multiple-sums/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 17 Feb 2020 05:20:54 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[backwards]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6337</guid>

					<description><![CDATA[<p>Given an array of integers&#160;target. From a starting array,&#160;A&#160;consisting of all 1&#8217;s, you may perform the following procedure : let&#160;x&#160;be the sum of all elements&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/eetcode-1354-construct-target-array-with-multiple-sums/">花花酱 LeetCode 1354. Construct Target Array With Multiple Sums</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="花花酱 LeetCode 1354. Construct Target Array With Multiple Sums - 刷题找工作 EP309" width="500" height="281" src="https://www.youtube.com/embed/t9pa9kpb-wk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given an array of integers&nbsp;<code>target</code>. From a starting array,&nbsp;<code>A</code>&nbsp;consisting of all 1&#8217;s, you may perform the following procedure :</p>



<ul><li>let&nbsp;<code>x</code>&nbsp;be the sum of all elements currently in your array.</li><li>choose index&nbsp;<code>i</code>, such that&nbsp;<code>0 &lt;= i &lt; target.size</code>&nbsp;and set the value of&nbsp;<code>A</code>&nbsp;at index&nbsp;<code>i</code>&nbsp;to&nbsp;<code>x</code>.</li><li>You may repeat this procedure&nbsp;as many times as needed.</li></ul>



<p>Return True if it is possible to construct the&nbsp;<code>target</code>&nbsp;array from&nbsp;<code>A</code>&nbsp;otherwise&nbsp;return False.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [9,3,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> Start with [1, 1, 1] 
[1, 1, 1], sum = 3 choose index 1
[1, 3, 1], sum = 5 choose index 2
[1, 3, 5], sum = 9 choose index 0
[9, 3, 5] Done
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [1,1,1,2]
<strong>Output:</strong> false
<strong>Explanation:</strong> Impossible to create target array from [1,1,1,1].
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> target = [8,5]
<strong>Output:</strong> true
</pre>



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



<ul><li><code>N == target.length</code></li><li><code>1 &lt;= target.length&nbsp;&lt;= 5 * 10^4</code></li><li><code>1 &lt;= target[i] &lt;= 10^9</code></li></ul>



<h2><strong>Solution: Backwards Simulation</strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1354-ep309.png" alt="" class="wp-image-6347" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1354-ep309.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1354-ep309-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/02/1354-ep309-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



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



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



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



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



<p>Start with the largest number in the array, since it should be the sum of all previous numbers.</p>



<p>[9,3,5] => [9 &#8211; (3+5), 3, 5] => [1, 3, 5] => [1, 3, 5 &#8211; (1+3)] => [1, 3, 1] => [1, 3 &#8211; (1+1), 1] => [1,1,1] done</p>



<p>Time complexity: O(n + log(n*t)*logn)<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:
  bool isPossible(vector&lt;int&gt;&amp; target) {
    priority_queue&lt;int&gt; q(begin(target), end(target));
    long sum = accumulate(begin(target), end(target), 0LL);    
    while (true) {      
      int t = q.top(); q.pop();
      sum -= t;
      if (t == 1 || sum == 1) return true;      
      if (t &lt; sum || sum == 0 || t % sum == 0) return false;
      t %= sum;
      sum += t;
      q.push(t);
    }
    return true;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/eetcode-1354-construct-target-array-with-multiple-sums/">花花酱 LeetCode 1354. Construct Target Array With Multiple Sums</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/simulation/eetcode-1354-construct-target-array-with-multiple-sums/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
