O(N*logN) ➡️ O(N) || Full explained || Beats 100% || [Java/C++/Python/Go/Ruby]
Intuition\n1. We need to increase the mass of the planet by adding the mass of asteriod to it.\n2. But we need to take care that the mass of asteroid should not
# Intuition\n1. We need to increase the mass of the planet by adding the mass of asteriod to it.\n2. But we need to take care that the mass of asteroid should not be greater than the planet, otherwise planet will be destroyed.\n3. So we will add the mass of smaller asteriod.\n\n# Approach Brute\n1. **Sorting:** The array of asteroid masses (asteroids) is sorted in ascending order using Arrays.sort(asteroids).\n2. **Initialization:** A variable mass is initialized with the provided initialMass value, cast to a long to handle potential overflow during summation.\n3. **Iteration through Asteroids:**\n    - The code iterates through each asteroid in the sorted array.\n    - For each asteroid:\n        If the asteroid\'s mass is greater than the current total mass (mass), the method returns false immediately, indicating that the mass is insufficient to destroy the remaining asteroids.\n        Otherwise, the asteroid\'s mass is added to the total mass (mass).\n4. **Completion:** If the loop completes without returning false, it implies that the mass, combined with the masses of the asteroids, is enough to destroy all asteroids. The method returns true.\n\n\n# Complexity\n- Time complexity: $$O(n*logn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n\n         Arrays.sort(asteroids);\n         long mass = (long)mas;\n\n         for(int i=0; i<asteroids.length; i++){\n             if(asteroids[i] > mass){\n                 return false;\n             }\n             else {\n                 mass += asteroids[i];\n             }\n         }\n         return true;        \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int initialMass, vector<int>& asteroids) {\n        sort(asteroids.begin(), asteroids.end());\n        long mass = static_cast<long>(initialMass);\n\n        for (int i = 0; i < asteroids.size(); i++) {\n            if (asteroids[i] > mass) {\n                return false;\n            } else {\n                mass += asteroids[i];\n            }\n        }\n\n        return true;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def asteroidsDestroyed(self, initial_mass: int, asteroids: List[int]) -> bool:\n        asteroids.sort()\n        total_mass = initial_mass\n\n        for asteroid in asteroids:\n            if asteroid > total_mass:\n                return False\n            else:\n                total_mass += asteroid\n\n        return True\n```\n```JavaScript []\n/**\n * @param {number} mass\n * @param {number[]} asteroids\n * @return {boolean}\n */\nvar asteroidsDestroyed = function(mass, asteroids) {\n    asteroids.sort((a, b) => a - b);\n    let totalMass = mass;\n\n    for (const asteroid of asteroids) {\n        if (asteroid > totalMass) {\n            return false;\n        } else {\n            totalMass += asteroid;\n        }\n    }\n\n    return true;\n};\n```\n```Go []\npackage main\n\nimport (\n\t"fmt"\n\t"sort"\n)\n\nfunc asteroidsDestroyed(mass int, asteroids []int) bool {\n\tsort.Ints(asteroids)\n\ttotalMass := mass\n\n\tfor _, asteroid := range asteroids {\n\t\tif asteroid > totalMass {\n\t\t\treturn false\n\t\t} else {\n\t\t\ttotalMass += asteroid\n\t\t}\n\t}\n\n\treturn true\n}\n```\n```Ruby []\n# @param {Integer} mass\n# @param {Integer[]} asteroids\n# @return {Boolean}\ndef asteroids_destroyed(mass, asteroids)\n  asteroids.sort!\n  total_mass = mass\n\n  asteroids.each do |asteroid|\n    if asteroid > total_mass\n      return false\n    else\n      total_mass += asteroid\n    end\n  end\n\n  true\nend\n```\n\n---\n\n\n---\n\n\n# Approach Optimal\n1. **Initialization:**\n    - Initialize a variable newMass with the provided mass value, cast to a long to handle potential overflow during summation.\n    - Create an empty stack (`st`) to simulate the asteroids\' positions.\n2. **Iteration through Asteroids:**\n    - Iterate through each asteroid in the array asteroids.\n    - For each asteroid (`val`):\n        - Push the asteroid\'s mass onto the stack.\n        - Enter a while loop as long as the stack is not empty and the top asteroid\'s mass (st.peek()) is less than or equal to newMass.\n        - Inside the loop, pop the top asteroid from the stack and add its mass to newMass.\n3. **Completion Check:**\n    - After processing all asteroids, check if the stack is empty.\n    - If the stack is empty, it means all the asteroids have been destroyed, and the method returns true.\n    - If the stack is not empty, it means some asteroids are left, and the method returns false.\n    - # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n# Code\n```Java []\nclass Solution {\n    public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n        long newMass = mass;\n        Stack<Integer> st = new Stack<>();\n\n        for(int val : asteroids) {\n            st.push(val);\n            while(!st.isEmpty() && st.peek() <= newMass) {\n                newMass += st.pop();\n            }\n        }\n        return st.isEmpty();\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n        long newMass = mass;\n        stack<int> st;\n\n        for (int val : asteroids) {\n            st.push(val);\n            while (!st.empty() && st.top() <= newMass) {\n                newMass += st.top();\n                st.pop();\n            }\n        }\n        return st.empty();\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:\n        new_mass = mass\n        stack = []\n\n        for val in asteroids:\n            stack.append(val)\n            while stack and stack[-1] <= new_mass:\n                new_mass += stack.pop()\n\n        return not stack\n```\n```JavaScript []\n/**\n * @param {number} mass\n * @param {number[]} asteroids\n * @return {boolean}\n */\nvar asteroidsDestroyed = function(mass, asteroids) {\n    let newMass = mass;\n    let stack = [];\n\n    for (let val of asteroids) {\n        stack.push(val);\n        while (stack.length > 0 && stack[stack.length - 1] <= newMass) {\n            newMass += stack.pop();\n        }\n    }\n\n    return stack.length === 0;\n};\n```
['C++', 'Java', 'Go', 'Python3', 'Ruby', 'JavaScript']