◀ THE GRIND — STACK

Asteroid Collision

MEDIUM✓ CHIP-TIMEDLC #735 — FULL STATEMENT ↗

The drill: Simulate a row of asteroids drifting left or right — sign gives direction, magnitude gives size. Same-size collisions destroy both; unequal ones destroy the smaller. Return whatever survives, left to right.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A row of asteroids arrives as an array of nonzero integers. Each value's sign gives its direction — positive drifts right, negative drifts left — and its magnitude gives its size.

Two asteroids collide only when a right-moving one is immediately followed, later in the row, by a left-moving one closing the gap between them. On collision, the smaller one is destroyed; equal sizes destroy both.

Asteroids moving the same direction never catch each other and never collide. The drill hands back whatever asteroids remain once every possible collision has played out, left to right.

EX 01
asteroids = [5, 10, -5]
[5, 10]
SMALLER ASTEROID DESTROYED ON IMPACT
EX 02
asteroids = [8, -8]
[]
EQUAL SIZES MUTUALLY DESTROY
EX 03
asteroids = [10, 2, -5]
[10]
LARGE SURVIVOR EATS A CHAIN OF SMALLER RIGHT-MOVERS
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

A collision only ever happens between a right-mover and the very next left-mover after it — same-direction asteroids never catch each other. What needs to be remembered as you scan?

HINT 2 THE STRUCTURE

A stack holds “survivors confirmed so far.” A new left-mover only has to fight the top of the stack if that top is moving right; keep resolving until it can't fight (or the stack empties), then push whatever's left of it.

HINT 3 ONE STEP FROM THE ANSWER

For each value: while the top is a smaller right-mover than an incoming left-mover, pop it (destroyed). If the top's magnitude equals the incomer's, pop it and the incomer too. Otherwise, if the top is a larger or equal right-mover, the incomer is destroyed and nothing pushes.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE SURVIVORS STACKPATTERN · STACK OF SURVIVORSasteroids = [5, 10, -6, -10, -20]
5
10
-6
-10
-20
SURVIVOR STACK
— empty —
STEP 1

Right-movers push freely; a left-mover fights the top of the stack while that top is a smaller or equal right-mover.

STEP 1 / 7 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/asteroid-collision.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        stack = []
        for a in asteroids:
            alive = True
            while alive and a < 0 and stack and stack[-1] > 0:
                if stack[-1] < -a:
                    stack.pop()
                    continue
                elif stack[-1] == -a:
                    stack.pop()
                alive = False
            if alive:
                stack.append(a)
        return stack
TIME O(N)SPACE O(N)PYTHON · RACE PACE · 15 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED