{"id":4376,"date":"2018-11-29T20:13:05","date_gmt":"2018-11-30T04:13:05","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4376"},"modified":"2018-11-29T20:13:19","modified_gmt":"2018-11-30T04:13:19","slug":"leetcode-946-validate-stack-sequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-946-validate-stack-sequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 946. Validate Stack Sequences"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given two sequences\u00a0<code>pushed<\/code>\u00a0and\u00a0<code>popped<\/code>\u00a0<strong>with distinct values<\/strong>,\u00a0return\u00a0<code>true<\/code>\u00a0if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>pushed = <span id=\"example-input-1-1\">[1,2,3,4,5]<\/span>, popped = <span id=\"example-input-1-2\">[4,5,3,2,1]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">true<\/span>\r\n<strong>Explanation: <\/strong>We might do the following sequence:\r\npush(1), push(2), push(3), push(4), pop() -&gt; 4,\r\npush(5), pop() -&gt; 5, pop() -&gt; 3, pop() -&gt; 2, pop() -&gt; 1\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>pushed = <span id=\"example-input-2-1\">[1,2,3,4,5]<\/span>, popped = <span id=\"example-input-2-2\">[4,3,5,1,2]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">false<\/span>\r\n<strong>Explanation: <\/strong>1 cannot be popped before 2.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= pushed.length == popped.length &lt;= 1000<\/code><\/li>\n<li><code>0 &lt;= pushed[i], popped[i] &lt; 1000<\/code><\/li>\n<li><code>pushed<\/code>\u00a0is a permutation of\u00a0<code>popped<\/code>.<\/li>\n<li><code>pushed<\/code>\u00a0and\u00a0<code>popped<\/code>\u00a0have distinct values.<\/li>\n<\/ol>\n<h1><strong>Solution: Simulation<\/strong><\/h1>\n<p>Simulate the push\/pop operation.<\/p>\n<p>Push element from |pushed sequence| onto stack s one by one and pop when top of the stack s is equal the current element in the |popped sequence|.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 8 ms\r\nclass Solution {\r\npublic:\r\n  bool validateStackSequences(vector&lt;int&gt;&amp; pushed, vector&lt;int&gt;&amp; popped) {\r\n    stack&lt;int&gt; s;\r\n    auto it = begin(popped);\r\n    for (int e : pushed) {\r\n      s.push(e);\r\n      while (!s.empty() &amp;&amp; s.top() == *it) {\r\n        s.pop();\r\n        ++it;\r\n      }\r\n    }\r\n    return it == end(popped);\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \"># Author: Huahua, Running time: 40 ms\r\nclass Solution:\r\n  def validateStackSequences(self, pushed, popped):\r\n    s = []\r\n    i = 0\r\n    for e in pushed:\r\n      s.append(e)\r\n      while s and s[-1] == popped[i]:\r\n        s.pop()\r\n        i += 1\r\n    return i == len(popped)<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two sequences\u00a0pushed\u00a0and\u00a0popped\u00a0with distinct values,\u00a0return\u00a0true\u00a0if and only if this could have been the result of a sequence of push and pop operations on an&#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,407],"tags":[177,179,180],"class_list":["post-4376","post","type-post","status-publish","format-standard","hentry","category-simulation","category-stack","tag-medium","tag-simulation","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4376","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=4376"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4376\/revisions"}],"predecessor-version":[{"id":4378,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4376\/revisions\/4378"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}