{"id":1089,"date":"2017-12-03T16:30:46","date_gmt":"2017-12-04T00:30:46","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1089"},"modified":"2017-12-11T22:28:53","modified_gmt":"2017-12-12T06:28:53","slug":"leetcode-735-asteroid-collision","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-735-asteroid-collision\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 735. Asteroid Collision"},"content":{"rendered":"<div class=\"question-description\">\n<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 735. Asteroid Collision - \u5237\u9898\u627e\u5de5\u4f5c EP122\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/8oOhXcdXbZk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>We are given an array\u00a0<code>asteroids<\/code>\u00a0of integers representing asteroids in a row.<\/p>\n<p>For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.<\/p>\n<p>Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \r\nasteroids = [5, 10, -5]\r\nOutput: [5, 10]\r\nExplanation: \r\nThe 10 and -5 collide resulting in 10.  The 5 and 10 never collide.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \r\nasteroids = [8, -8]\r\nOutput: []\r\nExplanation: \r\nThe 8 and -8 collide exploding each other.\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\">Input: \r\nasteroids = [10, 2, -5]\r\nOutput: [10]\r\nExplanation: \r\nThe 2 and -5 collide resulting in -5.  The 10 and -5 collide resulting in 10.\r\n<\/pre>\n<p><b>Example 4:<\/b><\/p>\n<pre class=\"\">Input: \r\nasteroids = [-2, -1, 1, 2]\r\nOutput: [-2, -1, 1, 2]\r\nExplanation: \r\nThe -2 and -1 are moving left, while the 1 and 2 are moving right.\r\nAsteroids moving the same direction never meet, so no asteroids will meet each other.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>The length of\u00a0<code>asteroids<\/code>\u00a0will be at most\u00a0<code>10000<\/code>.<\/li>\n<li>Each asteroid will be a non-zero integer in the range\u00a0<code>[-1000, 1000].<\/code>.<\/li>\n<\/ul>\n<\/div>\n<div id=\"interviewed-div\"><strong>Idea:<\/strong><\/div>\n<div>Simulation<\/div>\n<div>\n<p><span style=\"font-weight: 400;\">Time complexity: O(n), Space complexity: O(n)<\/span><\/p>\n<\/div>\n<div><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/735-ep122-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1095\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/735-ep122-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/735-ep122-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/735-ep122-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/735-ep122-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/div>\n<p><strong>Solution:<\/strong><\/p>\n<p>C ++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 22 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; asteroidCollision(vector&lt;int&gt;&amp; asteroids) {\r\n        vector&lt;int&gt; s;\r\n        for (int i = 0 ; i &lt; asteroids.size(); ++i) {\r\n            const int size = asteroids[i];\r\n            if (size &gt; 0) { \/\/ To right, OK\r\n                s.push_back(size);\r\n            } else {\r\n                \/\/ To left\r\n                if (s.empty() || s.back() &lt; 0) \/\/ OK when all are negtives\r\n                    s.push_back(size);\r\n                else if (abs(s.back()) &lt;= abs(size)) {\r\n                    \/\/ Top of the stack is going right.\r\n                    \/\/ Its size is less than the current one.\r\n                    \r\n                    \/\/ The current one still alive!\r\n                    if (abs(s.back()) &lt; abs(size)) --i;\r\n                    \r\n                    s.pop_back(); \/\/ Destory the top one moving right\r\n                }                    \r\n            }\r\n        }\r\n        \r\n        \/\/ s must look like [-s1, -s2, ... , si, sj, ...]\r\n        return s;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: We are given an array\u00a0asteroids\u00a0of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164,48],"tags":[179,180],"class_list":["post-1089","post","type-post","status-publish","format-standard","hentry","category-medium","category-simulation","tag-simulation","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1089","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=1089"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"predecessor-version":[{"id":1097,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1089\/revisions\/1097"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}