<?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>exchange Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/exchange/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/exchange/</link>
	<description></description>
	<lastBuildDate>Mon, 20 Aug 2018 03:05:46 +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>exchange Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/exchange/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 888. Fair Candy Swap</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-888-fair-candy-swap/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-888-fair-candy-swap/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 20 Aug 2018 03:05:37 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[hashtable]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3634</guid>

					<description><![CDATA[<p>Problem Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-888-fair-candy-swap/">花花酱 LeetCode 888. Fair Candy Swap</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Alice and Bob have candy bars of different sizes: <code>A[i]</code> is the size of the <code>i</code>-th bar of candy that Alice has, and <code>B[j]</code> is the size of the <code>j</code>-th bar of candy that Bob has.</p>
<p>Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy.  (<em>The total amount of candy a person has is the sum of the sizes of candy bars they have.</em>)</p>
<p>Return an integer array <code>ans</code> where <code>ans[0]</code> is the size of the candy bar that Alice must exchange, and <code>ans[1]</code> is the size of the candy bar that Bob must exchange.</p>
<p>If there are multiple answers, you may return any one of them.  It is guaranteed an answer exists.</p>
<div>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-1-1">[1,1]</span>, B = <span id="example-input-1-2">[2,2]</span>
<strong>Output: </strong><span id="example-output-1">[1,2]</span>
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-2-1">[1,2]</span>, B = <span id="example-input-2-2">[2,3]</span>
<strong>Output: </strong><span id="example-output-2">[1,2]</span>
</pre>
<div>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-3-1">[2]</span>, B = <span id="example-input-3-2">[1,3]</span>
<strong>Output: </strong><span id="example-output-3">[2,3]</span>
</pre>
<div>
<p><strong>Example 4:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>A = <span id="example-input-4-1">[1,2,5]</span>, B = <span id="example-input-4-2">[2,4]</span>
<strong>Output: </strong><span id="example-output-4">[5,4]</span>
</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>1 &lt;= A.length &lt;= 10000</code></li>
<li><code>1 &lt;= B.length &lt;= 10000</code></li>
<li><code>1 &lt;= A[i] &lt;= 100000</code></li>
<li><code>1 &lt;= B[i] &lt;= 100000</code></li>
<li>It is guaranteed that Alice and Bob have different total amounts of candy.</li>
<li>It is guaranteed there exists an answer.</li>
</ul>
<p>&nbsp;</p>
<h1><strong>Solution: HashTable</strong></h1>
<p>Time complexity: O(A+B)</p>
<p>Space complexity: O(B)</p>
<p>Clean version</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 132 ms
class Solution {
public:
  vector&lt;int&gt; fairCandySwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {    
    int sum_a = accumulate(begin(A), end(A), 0);
    int sum_b = accumulate(begin(B), end(B), 0);
    unordered_set&lt;int&gt; s(begin(B), end(B));    
    int diff = (sum_b - sum_a) / 2;
    for (int a : A)
      if (s.count(a + diff)) return {a, a + diff};
  }
};</pre><p>Faster version</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 68 ms
class Solution {
public:
  vector&lt;int&gt; fairCandySwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {    
    int sum_a = accumulate(begin(A), end(A), 0);
    int sum_b = 0;
    unordered_set&lt;int&gt; s;
    for (int b : B) {
      sum_b += b;
      s.insert(b);
    }
    int diff = (sum_b - sum_a) / 2;
    for (int a : A)
      if (s.count(a + diff)) return {a, a + diff};
  }
};</pre><p>&nbsp;</p>
</div>
</div>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-888-fair-candy-swap/">花花酱 LeetCode 888. Fair Candy Swap</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-888-fair-candy-swap/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
