Standard Bar Stack and Queues · Monotonic Stack and Queue O(n) · O(n)
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.
2 <= asteroids.length <= 10^4-1000 <= asteroids[i] <= 1000asteroids[i] != 0Input: {"asteroids":[5,10,-5]}
Output: [5,10]
10 absorbs the incoming -5; the two right-movers never meet.
Input: {"asteroids":[8,-8]}
Output: []
Equal masses annihilate on contact, leaving nothing.