{"id":9234,"date":"2021-12-25T18:20:42","date_gmt":"2021-12-26T02:20:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9234"},"modified":"2021-12-25T18:21:52","modified_gmt":"2021-12-26T02:21:52","slug":"leetcode-1920-build-array-from-permutation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1920-build-array-from-permutation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1920. Build Array from Permutation"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>zero-based permutation<\/strong>&nbsp;<code>nums<\/code>&nbsp;(<strong>0-indexed<\/strong>), build an array&nbsp;<code>ans<\/code>&nbsp;of the&nbsp;<strong>same length<\/strong>&nbsp;where&nbsp;<code>ans[i] = nums[nums[i]]<\/code>&nbsp;for each&nbsp;<code>0 &lt;= i &lt; nums.length<\/code>&nbsp;and return it.<\/p>\n\n\n\n<p>A&nbsp;<strong>zero-based permutation<\/strong>&nbsp;<code>nums<\/code>&nbsp;is an array of&nbsp;<strong>distinct<\/strong>&nbsp;integers from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>nums.length - 1<\/code>&nbsp;(<strong>inclusive<\/strong>).<\/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> nums = [0,2,1,5,3,4]\n<strong>Output:<\/strong> [0,1,2,4,5,3]<strong>\nExplanation:<\/strong> The array ans is built as follows: \nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n    = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]\n    = [0,1,2,4,5,3]<\/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> nums = [5,0,1,2,3,4]\n<strong>Output:<\/strong> [4,5,0,1,2,3]\n<strong>Explanation:<\/strong> The array ans is built as follows:\nans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]\n    = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]\n    = [4,5,0,1,2,3]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>0 &lt;= nums[i] &lt; nums.length<\/code><\/li><li>The elements in&nbsp;<code>nums<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><\/ul>\n\n\n\n<p><strong>Follow-up:<\/strong>&nbsp;Can you solve it without using an extra space (i.e.,&nbsp;<code>O(1)<\/code>&nbsp;memory)?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Straight forward<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> buildArray(vector<int>& nums) {\n    vector<int> ans(nums);\n    for (size_t i = 0; i < nums.size(); ++i)\n      ans[i] = nums[nums[i]];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Follow up: Inplace Encoding<\/strong><\/h2>\n\n\n\n<p>Since nums[i] &lt;= 1000, we can use low 16 bit to store the original value and high 16 bit for new value.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> buildArray(vector<int>& nums) {\n    const int n = nums.size();\n    for (int i = 0; i < n; ++i)\n      nums[i] |= (nums[nums[i]] &#038; 0xffff) << 16;\n    for (int i = 0; i < n; ++i)\n      nums[i] >>= 16;\n    return nums;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;zero-based permutation&nbsp;nums&nbsp;(0-indexed), build an array&nbsp;ans&nbsp;of the&nbsp;same length&nbsp;where&nbsp;ans[i] = nums[nums[i]]&nbsp;for each&nbsp;0 &lt;= i &lt; nums.length&nbsp;and return it. A&nbsp;zero-based permutation&nbsp;nums&nbsp;is an array of&nbsp;distinct&nbsp;integers from&nbsp;0&nbsp;to&nbsp;nums.length &#8211; 1&nbsp;(inclusive).&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,16,222],"class_list":["post-9234","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-bit","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9234","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=9234"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9234\/revisions"}],"predecessor-version":[{"id":9237,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9234\/revisions\/9237"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}