Skip to content

Practice Asteroid Collision

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#735
LeetCode Problem

Asteroid Collision

Problem Statement

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.

Source: Asteroid Collision on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
asteroids = [5,10,-5]
Output
[5,10]

# Example 2

Input
asteroids = [8,-8]
Output
[]

# Example 3

Input
asteroids = [10,2,-5]
Output
[10]

Constraints

2 <= asteroids.length <= 10^4
-1000 <= asteroids[i] <= 1000
asteroids[i] != 0

Related LeetCode Problems

Practice more problems similar to Asteroid Collision.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.