{"id":8766,"date":"2021-11-25T12:57:49","date_gmt":"2021-11-25T20:57:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8766"},"modified":"2021-11-25T13:18:47","modified_gmt":"2021-11-25T21:18:47","slug":"leetcode-2007-find-original-array-from-doubled-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2007-find-original-array-from-doubled-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2007. Find Original Array From Doubled Array"},"content":{"rendered":"\n<p>An integer array&nbsp;<code>original<\/code>&nbsp;is transformed into a&nbsp;<strong>doubled<\/strong>&nbsp;array&nbsp;<code>changed<\/code>&nbsp;by appending&nbsp;<strong>twice the value<\/strong>&nbsp;of every element in&nbsp;<code>original<\/code>, and then randomly&nbsp;<strong>shuffling<\/strong>&nbsp;the resulting array.<\/p>\n\n\n\n<p>Given an array&nbsp;<code>changed<\/code>, return&nbsp;<code>original<\/code><em>&nbsp;if&nbsp;<\/em><code>changed<\/code><em>&nbsp;is a&nbsp;<strong>doubled<\/strong>&nbsp;array. If&nbsp;<\/em><code>changed<\/code><em>&nbsp;is not a&nbsp;<strong>doubled<\/strong>&nbsp;array, return an empty array. The elements in<\/em>&nbsp;<code>original<\/code>&nbsp;<em>may be returned in&nbsp;<strong>any<\/strong>&nbsp;order<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> changed = [1,3,4,2,6,8]\n<strong>Output:<\/strong> [1,3,4]\n<strong>Explanation:<\/strong> One possible original array could be [1,3,4]:\n- Twice the value of 1 is 1 * 2 = 2.\n- Twice the value of 3 is 3 * 2 = 6.\n- Twice the value of 4 is 4 * 2 = 8.\nOther original arrays could be [4,3,1] or [3,1,4].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> changed = [6,3,0,1]\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> changed is not a doubled array.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> changed = [1]\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> changed is not a doubled array.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= changed.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= changed[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Multiset<\/strong><\/h2>\n\n\n\n<p>Start from the smallest number x, erase one x * 2 from the set.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Multiset<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> findOriginalArray(vector<int>& changed) {\n    if (changed.size() & 1) return {};\n    const int n = changed.size() \/ 2;\n    multiset<int> s(begin(changed), end(changed));    \n    while (ans.size() != n) {\n      int o = *begin(s);\n      s.erase(begin(s));\n      ans.push_back(o);\n      auto it = s.find(o * 2);\n      if (it == end(s)) return {};\n      s.erase(it);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(max(nums) + n)<br>Space complexity: O(max(nums))<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Hashtable<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> findOriginalArray(vector<int>& changed) {\n    if (changed.size() & 1) return {};\n    const int n = changed.size() \/ 2;\n    const int kMax = *max_element(begin(changed), end(changed));\n    vector<int> m(kMax + 1);\n    for (int x : changed) ++m[x];\n    if (m[0] & 1) return {};\n    vector<int> ans(m[0] \/ 2, 0);\n    for (int x = 1; ans.size() != n; ++x) {\n      if (x * 2 > kMax || m[x * 2] < m[x]) return {};      \n      ans.insert(end(ans), m[x], x);\n      m[x * 2] -= m[x];   \n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An integer array&nbsp;original&nbsp;is transformed into a&nbsp;doubled&nbsp;array&nbsp;changed&nbsp;by appending&nbsp;twice the value&nbsp;of every element in&nbsp;original, and then randomly&nbsp;shuffling&nbsp;the resulting array. Given an array&nbsp;changed, return&nbsp;original&nbsp;if&nbsp;changed&nbsp;is a&nbsp;doubled&nbsp;array. If&nbsp;changed&nbsp;is not a&nbsp;doubled&nbsp;array,&#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":[82,177,335,656],"class_list":["post-8766","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-multiset","tag-treeset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8766","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=8766"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8766\/revisions"}],"predecessor-version":[{"id":8774,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8766\/revisions\/8774"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}