← DiffPush

Asteroid Collision

Standard Bar Stack and Queues · Monotonic Stack and Queue O(n) · O(n)

The Particle Beam Filter

A physics rig fires particles down a straight tube: positives travel right, negatives travel left, all at identical speed, and magnitude encodes mass. Head-on meetings destroy the lighter particle outright — equal masses annihilate both. The safety log must show which particles survive the entire sequence.

Input: An integer array asteroids where sign is direction and absolute value is mass.

Output: Return the array of surviving particles in their original relative order.

Constraints

Examples

Example 1

Input: {"asteroids":[5,10,-5]}
Output: [5,10]
10 absorbs the incoming -5; the two right-movers never meet.

Example 2

Input: {"asteroids":[8,-8]}
Output: []
Equal masses annihilate on contact, leaving nothing.

Solve this in your browser →

Also on LeetCode ↗