✅ [C++/Python/Java] Simple Solution || Sort and Greedy || Detailed Explanation
PLEASE UPVOTE if you like \uD83D\uDE01 If you have any question, feel free to ask. \n In each round we choose the smallest asteroid to destroy and gain its mass
**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.** \n* In each round we choose the smallest asteroid to destroy and gain its mass.\n\t* **Before facing an asteroid, the best preparation we can make is to destroy all astrioids smaller than it and gain their total mass.**\n* So we need to sort the asteroids first.\n* Note that the sum of astroids may exceed int32, so either we use int64 to represent the sum, or stop when it reaches 100,000 because the maximum of astroids is 100,000\n\t* each has its own advantages, check `if mass >= 100000` may stop earlier.\n\n```\nTime  Complexity: O(NlogN) for sorting\nSpace Complexity: O(1) if we ignore the space cost of sorting\n```\n\n**Python**\n```\nclass Solution(object):\n    def asteroidsDestroyed(self, mass, asteroids):\n        asteroids.sort()\n        for n in asteroids:\n            if mass < n: return False\n            if mass >= 100000: return True\n            mass += n\n        return True\n```\n```\nclass Solution(object):\n    def asteroidsDestroyed(self, mass, asteroids):\n        asteroids.sort()\n        for n in asteroids:\n            if mass < n: return False\n            mass += n\n        return True\n        \n```\n**C++**\n```\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n        sort(asteroids.begin(),asteroids.end());\n        for (int i = 0; i < asteroids.size(); i ++) {\n            if (mass >= 100000) return true;\n            if (mass < asteroids[i]) return false;\n            mass += asteroids[i];\n        }\n        return true;\n    }\n};\n```\n```\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n        long long curr_mass = mass;\n        sort(asteroids.begin(),asteroids.end());\n        for (int i = 0; i < asteroids.size(); i ++) {\n            if (curr_mass < asteroids[i]) return false;\n            curr_mass += asteroids[i];\n        }\n        return true;\n    }\n};\n```\n\n**Java**\n```\nclass Solution {\n    public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n        Arrays.sort(asteroids);\n        for (int i = 0; i < asteroids.length; i ++) {\n            if (mass >= 100000) return true;\n            if (mass < asteroids[i]) return false;\n            mass += asteroids[i];\n        }\n        return true;\n    }\n}\n```\n```\nclass Solution {\n    public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n        long curr_mass = mass;\n        Arrays.sort(asteroids);\n        for (int i = 0; i < asteroids.length; i ++) {\n            if (curr_mass < asteroids[i]) return false;\n            curr_mass += asteroids[i];\n        }\n        return true;\n    }\n}\n```\n**PLEASE UPVOTE if you like** \uD83D\uDE01 **If you have any question, feel free to ask.**
['C', 'Python', 'Java']