2126. Destroying Asteroids
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 53% Topics: Array, Greedy, Sorting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(nlogn)
# Space: O(1)
class Solution(object):
def asteroidsDestroyed(self, mass, asteroids):
"""
:type mass: int
:type asteroids: List[int]
:rtype: bool
"""
asteroids.sort()
for x in asteroids:
if x > mass:
return False
mass += min(x, asteroids[-1]-mass)
return True
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions