{"id":6736,"date":"2020-05-11T13:19:14","date_gmt":"2020-05-11T20:19:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6736"},"modified":"2020-05-11T13:19:49","modified_gmt":"2020-05-11T20:19:49","slug":"leetcode-1441-build-an-array-with-stack-operations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1441-build-an-array-with-stack-operations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1441. Build an Array With Stack Operations"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>target<\/code>&nbsp;and&nbsp;an integer&nbsp;<code>n<\/code>. In each iteration, you will read a number from &nbsp;<code>list = {1,2,3..., n}<\/code>.<\/p>\n\n\n\n<p>Build the&nbsp;<code>target<\/code>&nbsp;array&nbsp;using the following operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Push<\/strong>: Read a new element from the beginning&nbsp;<code>list<\/code>, and push it in the array.<\/li><li><strong>Pop<\/strong>: delete the last element of&nbsp;the array.<\/li><li>If the target array is already&nbsp;built, stop reading more elements.<\/li><\/ul>\n\n\n\n<p>You are guaranteed that the target array is strictly&nbsp;increasing, only containing&nbsp;numbers between 1 to&nbsp;<code>n<\/code>&nbsp;inclusive.<\/p>\n\n\n\n<p>Return the operations to build the target array.<\/p>\n\n\n\n<p>You are guaranteed that the answer is unique.<\/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> target = [1,3], n = 3\n<strong>Output:<\/strong> [\"Push\",\"Push\",\"Pop\",\"Push\"]\n<strong>Explanation: \n<\/strong>Read number 1 and automatically push in the array -&gt; [1]\nRead number 2 and automatically push in the array then Pop it -&gt; [1]\nRead number 3 and automatically push in the array -&gt; [1,3]\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> target = [1,2,3], n = 3\n<strong>Output:<\/strong> [\"Push\",\"Push\",\"Push\"]\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> target = [1,2], n = 4\n<strong>Output:<\/strong> [\"Push\",\"Push\"]\n<strong>Explanation: <\/strong>You only need to read the first 2 numbers and stop.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> target = [2,3,4], n = 4\n<strong>Output:<\/strong> [\"Push\",\"Pop\",\"Push\",\"Push\",\"Push\"]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= target.length &lt;= 100<\/code><\/li><li><code>1 &lt;= target[i]&nbsp;&lt;= 100<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>target<\/code>&nbsp;is strictly&nbsp;increasing.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>For each number in target, keep discarding i if i != num by &#8220;Push&#8221; + &#8220;Pop&#8221;, until i == num. One more &#8220;Push&#8221;.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) or O(1) w\/o output.<\/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<string> buildArray(vector<int>& target, int n) {\n    vector<string> ans;\n    int i = 1;\n    for (int num : target) {      \n      while (i != num) {\n        ans.push_back(\"Push\");     \n        ans.push_back(\"Pop\");\n        ++i;\n      }\n      ans.push_back(\"Push\");\n      ++i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;target&nbsp;and&nbsp;an integer&nbsp;n. In each iteration, you will read a number from &nbsp;list = {1,2,3&#8230;, n}. Build the&nbsp;target&nbsp;array&nbsp;using the following operations: Push: Read a&#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,179,180],"class_list":["post-6736","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6736","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=6736"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6736\/revisions"}],"predecessor-version":[{"id":6738,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6736\/revisions\/6738"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}