{"id":9401,"date":"2022-01-02T00:49:30","date_gmt":"2022-01-02T08:49:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9401"},"modified":"2022-01-02T00:50:39","modified_gmt":"2022-01-02T08:50:39","slug":"leetcode-2126-destroying-asteroids","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2126-destroying-asteroids\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2126. Destroying Asteroids"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>mass<\/code>, which represents the original mass of a planet. You are further given an integer array&nbsp;<code>asteroids<\/code>, where&nbsp;<code>asteroids[i]<\/code>&nbsp;is the mass of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;asteroid.<\/p>\n\n\n\n<p>You can arrange for the planet to collide with the asteroids in&nbsp;<strong>any arbitrary order<\/strong>. If the mass of the planet is&nbsp;<strong>greater than or equal to<\/strong>&nbsp;the mass of the asteroid, the asteroid is&nbsp;<strong>destroyed<\/strong>&nbsp;and the planet&nbsp;<strong>gains<\/strong>&nbsp;the mass of the asteroid. Otherwise, the planet is destroyed.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<strong>all<\/strong>&nbsp;asteroids can be destroyed. Otherwise, return&nbsp;<\/em><code>false<\/code><em>.<\/em><\/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> mass = 10, asteroids = [3,9,19,5,21]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> One way to order the asteroids is [9,19,5,3,21]:\n- The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19\n- The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38\n- The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43\n- The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46\n- The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67\nAll asteroids are destroyed.\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> mass = 5, asteroids = [4,9,23,4]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> \nThe planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.\nAfter the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.\nThis is less than 23, so a collision would not destroy the last asteroid.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= mass &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= asteroids.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= asteroids[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort asteroids by weight. Note, mass can be very big (<span style=\"background-color: rgb(243, 243, 243); font-family: monospace; font-size: inherit;\">10<\/span><sup style=\"font-family: monospace;\">5<\/sup>*<meta charset=\"utf-8\"><span style=\"background-color: rgb(243, 243, 243); font-family: monospace; font-size: inherit;\">10<\/span><sup style=\"font-family: monospace;\">5<\/sup>), for C++\/Java, use long instead of int.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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  bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n    long long curr = mass;\n    sort(begin(asteroids), end(asteroids));\n    for (int a : asteroids) {\n      if (curr < a) return false;\n      curr += a;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;mass, which represents the original mass of a planet. You are further given an integer array&nbsp;asteroids, where&nbsp;asteroids[i]&nbsp;is the mass of the&nbsp;ith&nbsp;asteroid.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,23],"class_list":["post-9401","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9401","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=9401"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9401\/revisions"}],"predecessor-version":[{"id":9404,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9401\/revisions\/9404"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}