You are given an integer mass, which represents the original mass of a planet. You are further given an integer array asteroids, where asteroids[i] is the mass of the ith asteroid.
You can arrange for the planet to collide with the asteroids in any arbitrary order. If the mass of the planet is greater than or equal to the mass of the asteroid, the asteroid is destroyed and the planet gains the mass of the asteroid. Otherwise, the planet is destroyed.
Return true if all asteroids can be destroyed. Otherwise, return false.
Example 1:
Input: mass = 10, asteroids = [3,9,19,5,21] Output: true Explanation: One way to order the asteroids is [9,19,5,3,21]: - The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19 - The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38 - The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43 - The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46 - The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67 All asteroids are destroyed.
Example 2:
Input: mass = 5, asteroids = [4,9,23,4] Output: false Explanation: The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23. After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22. This is less than 23, so a collision would not destroy the last asteroid.
Constraints:
1 <= mass <= 1051 <= asteroids.length <= 1051 <= asteroids[i] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to destroying asteroids involves trying every single possible order in which you could destroy them. For each order, we check if it's possible to destroy all asteroids with the given mass, and then choose the 'best' order, if one exists.
Here's how the algorithm would work step-by-step:
def can_destroy_asteroids_brute_force(mass, asteroids):
import itertools
asteroid_permutations = list(itertools.permutations(asteroids))
for asteroid_order in asteroid_permutations:
current_mass = mass
can_destroy_all = True
for asteroid_size in asteroid_order:
# Check if current mass is enough
if current_mass < asteroid_size:
can_destroy_all = False
break
# Update mass after destroying asteroid
current_mass += asteroid_size
# If the asteroids in this ordering were destroyed, return true
if can_destroy_all:
return True
# If no ordering works, we return false
return FalseTo successfully destroy all asteroids, we need to sort them and then check if our current mass is sufficient to destroy them in order. The clever part is understanding that if we can't destroy an asteroid, we'll never be able to destroy any that are larger than it.
Here's how the algorithm would work step-by-step:
def destroying_asteroids(mass, asteroids):
asteroids.sort()
current_mass = mass
for asteroid_size in asteroids:
# If current mass can't destroy,
# it will never be able to destroy larger ones
if current_mass >= asteroid_size:
current_mass += asteroid_size
else:
return False
# Return true if all asteroids can be destroyed
return True| Case | How to Handle |
|---|---|
| Empty asteroids array | Return true immediately, as there are no asteroids to destroy so the planet trivially succeeds. |
| Mass is less than or equal to 0 | If mass is non-positive, immediately return false because the planet will never grow. |
| Asteroids array contains very large numbers | Consider using a language with arbitrary precision integers, or explicitly check for and handle integer overflow during mass updates and comparisons to prevent incorrect results. |
| Asteroids are sorted in descending order and planet's initial mass is small | This will lead to immediate failure as even the first asteroid's mass will be larger than the initial planet mass, causing the algorithm to terminate early. |
| All asteroids have the same mass, greater than the initial planet's mass | The planet will never grow, so return false. |
| Large number of asteroids (close to maximum allowed) | Ensure the sorting algorithm used has optimal time complexity (O(n log n)) to avoid exceeding time limits. |
| The planet can destroy all the asteroids except for the last one due to integer overflow | Use a larger integer type (e.g., long long in C++, long in Java) or check for overflow during mass updates to avoid incorrect comparison. |
| Asteroids array contains negative numbers | Clarify in the prompt if negative asteroids are valid, and if not, handle them as invalid input (e.g., by throwing an exception or returning an error code). |