{"id":6535,"date":"2020-03-22T13:52:58","date_gmt":"2020-03-22T20:52:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6535"},"modified":"2020-03-22T13:53:07","modified_gmt":"2020-03-22T20:53:07","slug":"leetcode-1389-create-target-array-in-the-given-order","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1389-create-target-array-in-the-given-order\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1389. Create Target Array in the Given Order"},"content":{"rendered":"\n<p>Given two arrays of integers&nbsp;<code>nums<\/code>&nbsp;and&nbsp;<code>index<\/code>. Your task is to create&nbsp;<em>target<\/em>&nbsp;array under the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Initially&nbsp;<em>target<\/em>&nbsp;array is empty.<\/li><li>From left to right read nums[i] and index[i], insert at index&nbsp;<code>index[i]<\/code>&nbsp;the value&nbsp;<code>nums[i]<\/code>&nbsp;in&nbsp;<em>target<\/em>&nbsp;array.<\/li><li>Repeat the previous step until there are no elements to read in&nbsp;<code>nums<\/code>&nbsp;and&nbsp;<code>index.<\/code><\/li><\/ul>\n\n\n\n<p>Return the&nbsp;<em>target<\/em>&nbsp;array.<\/p>\n\n\n\n<p>It is guaranteed that the insertion operations will be valid.<\/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,1,2,3,4], index = [0,1,2,2,1]\n<strong>Output:<\/strong> [0,4,1,3,2]\n<strong>Explanation:<\/strong>\nnums       index     target\n0            0        [0]\n1            1        [0,1]\n2            2        [0,1,2]\n3            2        [0,1,3,2]\n4            1        [0,4,1,3,2]\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> nums = [1,2,3,4,0], index = [0,1,2,3,0]\n<strong>Output:<\/strong> [0,1,2,3,4]\n<strong>Explanation:<\/strong>\nnums       index     target\n1            0        [1]\n2            1        [1,2]\n3            2        [1,2,3]\n4            3        [1,2,3,4]\n0            0        [0,1,2,3,4]\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> nums = [1], index = [0]\n<strong>Output:<\/strong> [1]\n<\/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, index.length &lt;= 100<\/code><\/li><li><code>nums.length == index.length<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 100<\/code><\/li><li><code>0 &lt;= index[i] &lt;= i<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n) ~ O(n^2)<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> createTargetArray(vector<int>& nums, vector<int>& index) {\n    vector<int> target;\n    for (int i = 0; i < nums.size(); ++i)\n      target.insert(begin(target) + index[i], nums[i]);\n    return target;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two arrays of integers&nbsp;nums&nbsp;and&nbsp;index. Your task is to create&nbsp;target&nbsp;array under the following rules: Initially&nbsp;target&nbsp;array is empty. From left to right read nums[i] and index[i],&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,324,179],"class_list":["post-6535","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-insert","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6535","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=6535"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6535\/revisions"}],"predecessor-version":[{"id":6537,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6535\/revisions\/6537"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}