16 · Stacks & Monotonic Stack

#735 Asteroid Collision

Mediumsolve on LeetCode ↗

frequently reported atPayPalOracleAmazonUberMicrosoftBloomberg

the problem

In plain words

A row of asteroids is given as integers. The absolute value is an asteroid's size; the sign is its direction (positive moves right, negative moves left). All move at the same speed. When two meet, the smaller one explodes; if they are the same size, both explode. Asteroids moving the same way never meet. Return what is left after every collision, in order.

in asteroids = [5,10,-5]

out [5,10]

−5 meets 10 and explodes. 5 and 10 move the same way, so they never meet.

in asteroids = [8,-8]

out []

Same size: both explode.

in asteroids = [10,2,-5]

out [10]

−5 destroys 2, then explodes against 10.

  • 2 ≤ asteroids.length ≤ 10⁴
  • −1000 ≤ asteroids[i] ≤ 1000
  • asteroids[i] ≠ 0

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #853
      Car Fleet
      Mediumin Stacks
    2. #20
      Valid Parentheses
      Easyin Stacks
    3. #739
      Daily Temperatures
      Mediumin Stacks