{"id":3634,"date":"2018-08-19T20:05:37","date_gmt":"2018-08-20T03:05:37","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3634"},"modified":"2018-08-19T20:05:46","modified_gmt":"2018-08-20T03:05:46","slug":"leetcode-888-fair-candy-swap","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-888-fair-candy-swap\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 888. Fair Candy Swap"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Alice and Bob have candy bars of different sizes:\u00a0<code>A[i]<\/code>\u00a0is the size of the\u00a0<code>i<\/code>-th bar of candy that Alice has, and\u00a0<code>B[j]<\/code>\u00a0is the size of the\u00a0<code>j<\/code>-th bar of candy that Bob has.<\/p>\n<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\u00a0amount of candy.\u00a0 (<em>The total amount of candy\u00a0a person has is the sum of the sizes of candy\u00a0bars they have.<\/em>)<\/p>\n<p>Return an integer array\u00a0<code>ans<\/code>\u00a0where\u00a0<code>ans[0]<\/code>\u00a0is the size of the candy bar that Alice must exchange, and\u00a0<code>ans[1]<\/code>\u00a0is the size of the candy bar that Bob must exchange.<\/p>\n<p>If there are multiple answers, you may return any one of them.\u00a0 It is guaranteed an answer exists.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[1,2]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[1,2]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">[2,3]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 4:<\/strong><\/p>\n<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>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">[5,4]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= A.length &lt;= 10000<\/code><\/li>\n<li><code>1 &lt;= B.length &lt;= 10000<\/code><\/li>\n<li><code>1 &lt;= A[i] &lt;= 100000<\/code><\/li>\n<li><code>1 &lt;= B[i] &lt;= 100000<\/code><\/li>\n<li>It is guaranteed that Alice and Bob have different total amounts of\u00a0candy.<\/li>\n<li>It is guaranteed there exists an\u00a0answer.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>Time complexity: O(A+B)<\/p>\n<p>Space complexity: O(B)<\/p>\n<p>Clean version<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 132 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; fairCandySwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {    \r\n    int sum_a = accumulate(begin(A), end(A), 0);\r\n    int sum_b = accumulate(begin(B), end(B), 0);\r\n    unordered_set&lt;int&gt; s(begin(B), end(B));    \r\n    int diff = (sum_b - sum_a) \/ 2;\r\n    for (int a : A)\r\n      if (s.count(a + diff)) return {a, a + diff};\r\n  }\r\n};<\/pre>\n<p>Faster version<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 68 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; fairCandySwap(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {    \r\n    int sum_a = accumulate(begin(A), end(A), 0);\r\n    int sum_b = 0;\r\n    unordered_set&lt;int&gt; s;\r\n    for (int b : B) {\r\n      sum_b += b;\r\n      s.insert(b);\r\n    }\r\n    int diff = (sum_b - sum_a) \/ 2;\r\n    for (int a : A)\r\n      if (s.count(a + diff)) return {a, a + diff};\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Alice and Bob have candy bars of different sizes:\u00a0A[i]\u00a0is the size of the\u00a0i-th bar of candy that Alice has, and\u00a0B[j]\u00a0is the size of the\u00a0j-th&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,222,384,82],"class_list":["post-3634","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-easy","tag-exchange","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3634"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3634\/revisions"}],"predecessor-version":[{"id":3636,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3634\/revisions\/3636"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}