Asteroid Collision

Medium

Topics
ArrayStackSimulation

Given nums of asteroids (absolute value = size, sign = direction: positive right, negative left), return the state after all collisions. Two moving toward each other: the smaller explodes; equal sizes both explode. Same direction never collide.

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 <= nums.length <= 10^4
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0
Run ⌘' · Submit ⌘⏎