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] <= 1000asteroids[i] != 0When 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 a straight line where space rocks are moving toward or away from each other. The brute force strategy involves repeatedly letting every rock take one step forward in its preferred direction, pausing to check if any two rocks crash into each other.
Here's how the algorithm would work step-by-step:
def asteroid_collision(asteroids):
current_index = 0
# We repeatedly scan from the beginning because a collision can cascade backwards
while current_index < len(asteroids) - 1:
current_rock = asteroids[current_index]
next_rock = asteroids[current_index + 1]
# A collision occurs only when a right-moving rock meets a left-moving rock
if current_rock > 0 and next_rock < 0:
if abs(current_rock) == abs(next_rock):
asteroids.pop(current_index + 1)
asteroids.pop(current_index)
current_index = max(0, current_index - 1)
elif abs(current_rock) < abs(next_rock):
asteroids.pop(current_index)
current_index = max(0, current_index - 1)
else:
asteroids.pop(current_index + 1)
else:
current_index += 1
return asteroidsThe optimal strategy is to process the space objects one by one from left to right, keeping track of the survivors as you go. When a new object traveling left meets an object traveling right, they clash and the heavier one destroys the lighter one.
Here's how the algorithm would work step-by-step:
def asteroidCollision(asteroid_list):
surviving_asteroids = []
for current_asteroid in asteroid_list:
# Collisions only occur when a right-moving object meets a left-moving object.
while surviving_asteroids and current_asteroid < 0 and surviving_asteroids[-1] > 0:
previous_asteroid = surviving_asteroids[-1]
if abs(previous_asteroid) < abs(current_asteroid):
surviving_asteroids.pop()
continue
elif abs(previous_asteroid) == abs(current_asteroid):
surviving_asteroids.pop()
# If the surviving object is larger, the incoming object is destroyed.
break
else:
surviving_asteroids.append(current_asteroid)
return surviving_asteroids| Case | How to Handle |
|---|---|
| Empty or null input array | Return the empty array immediately without processing since no collisions can occur. |
| Single element input array | Return the array as is because a single asteroid cannot collide with anything. |
| All asteroids moving in the same direction (all positive or all negative) | The stack will accumulate all elements without any collisions occurring, returning the original array. |
| Asteroids moving away from each other (negative followed by positive) | The stack preserves both elements since their trajectories diverge and they never meet. |
| Chain reaction collisions where a newly exposed asteroid destroys subsequent incoming ones | A while loop checking the stack ensures the current asteroid is continuously compared against all previous colliding elements. |
| Asteroids of identical absolute sizes colliding | Both asteroids destroy each other by popping the stack and preventing the current asteroid from being added. |
| Extremely large input size requiring linear time complexity | Using a stack data structure ensures each asteroid is pushed and popped at most once, achieving efficient O(N) time complexity. |
| Maximum possible integer values representing asteroid sizes | Standard integer comparisons are used safely without risk of overflow since we only compare magnitudes directly. |