LeetCode 735, Medium. Topics: Array, Stack, Simulation. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:
Problem
We are given an array asteroids of integers representing asteroids in a row. The indices of the asteroid 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, or two moving away from each other, will never meet.
Examples
Constraints
- 2 <= asteroids.length <= 10^4
- -1000 <= asteroids[i] <= 1000
asteroids[i] != 0
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Grind, NeetCode 250, NeetCode All. Last modified on September 7, 2026