Destroying asteroids
September 30, 2022
greedyProblem URL: Destroying asteroids
We can sort the arstroids and greedy simulate the process described in the problem statement. If at any point the mass is less that the asteroid, then we return false, otherwise if we can finish iterating over every asteroid, then we return true.
class Solution:
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
for a in sorted(asteroids):
if a > mass:
return False
mass += a
return True
Time Complexity: O(nlog(n))
Space Complexity: O(1)