We are given an array asteroids
of integers representing asteroids in a row. The indices of the asteriod in the array represent their relative position in space.
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Input: asteroids = [5,10,-5] Output: [5,10] Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
Example 2:
Input: asteroids = [8,-8] Output: [] Explanation: The 8 and -8 collide exploding each other.
Example 3:
Input: asteroids = [10,2,-5] Output: [10] Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
Constraints:
2 <= asteroids.length <= 104
-1000 <= asteroids[i] <= 1000
asteroids[i] != 0
When 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:
Imagine you have a series of asteroids moving in different directions and sizes. The brute force method essentially simulates every single collision that could possibly occur, one at a time, until no more collisions are possible. It's like watching the asteroids crash over and over until things settle down.
Here's how the algorithm would work step-by-step:
def asteroid_collision_brute_force(asteroids): collisions_happened = True
while collisions_happened:
collisions_happened = False
index = 0
while index < len(asteroids) - 1:
first_asteroid = asteroids[index]
second_asteroid = asteroids[index + 1]
# Check if asteroids are moving in opposite directions
if first_asteroid > 0 and second_asteroid < 0:
collisions_happened = True
# Determine which asteroid survives based on size
if abs(first_asteroid) > abs(second_asteroid):
asteroids.pop(index + 1)
elif abs(first_asteroid) < abs(second_asteroid):
asteroids.pop(index)
# If equal size, remove both asteroids
else:
asteroids.pop(index + 1)
asteroids.pop(index)
else:
index += 1
return asteroids
We want to simulate asteroids colliding in space. The core idea is to use a stack to keep track of the asteroids that haven't exploded yet. If a new asteroid collides with the previous one, we resolve the collision and decide which asteroids remain, if any.
Here's how the algorithm would work step-by-step:
def asteroid_collision(asteroids):
asteroid_stack = []
for new_asteroid in asteroids:
while asteroid_stack and new_asteroid < 0 and asteroid_stack[-1] > 0:
# Resolve potential collision
top_asteroid = asteroid_stack[-1]
if top_asteroid < abs(new_asteroid):
asteroid_stack.pop()
elif top_asteroid == abs(new_asteroid):
asteroid_stack.pop()
new_asteroid = 0 #Asteroid destroyed
break # Break inner while loop because asteroid is destroyed
else:
new_asteroid = 0 #Asteroid destroyed
break # Break inner while loop because asteroid is destroyed
# Only push if it survives collisions
if new_asteroid:
asteroid_stack.append(new_asteroid)
return asteroid_stack
Case | How to Handle |
---|---|
Empty or null input array | Return an empty array if the input is null or has zero length. |
Input array with only one element | Return the input array itself as there are no collisions possible. |
All asteroids moving in the same direction (all positive or all negative) | The stack will simply accumulate all asteroids and the final result is the original array. |
Large input array causing potential memory issues | The stack-based approach has O(n) space complexity, so ensure sufficient memory allocation for very large arrays. |
Integer overflow if asteroid sizes are very large and the collision comparison is not careful. | Use appropriate data types (e.g., long) to prevent potential integer overflow during comparisons. |
A series of collisions that eventually results in an empty stack. | The stack should be handled correctly, and the function should return an empty array at the end. |
Alternating positive and negative asteroids of increasing size that all collide. | The stack operations of adding and removing elements must be handled carefully to ensure the correct final state. |
An asteroid moving left collides with a larger series of asteroids all moving right. | The algorithm needs to continuously compare with asteroids in the stack until the left moving asteroid is destroyed, or the stack is empty, or the left moving asteroid is larger than the top of the stack. |